小程序
小游戏
企业微信
微信支付
扫描小程序码分享
这是提前定义的片段
aaa: function (equal) {
代码1。。。
return new Promise((resolve, reject) => {
res => {
resolve(res);
}
})
},
下面是在主程序里面调用
aaa().then(
)
现在代码1和代码2还是同步执行。。。
谁帮着怎么改下变成异步执行完代码1再执行代码2
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
代码1处理完了,再resolve
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
具体怎么弄呢?半路出家,异步和同步还没弄明白。。。
代码1
resolve(res)
编译提示 res is not defined
app.js? [sm]:401 Uncaught (in promise) ReferenceError: res is not defined
at app.js? [sm]:401
at Promise (<anonymous>)
at e.xx_city_getname (app.js? [sm]:344)
at e.<anonymous> (WAService.js:1)
at index.js? [sm]:215
at <anonymous>
你自己写的res,我又不知是啥
非常感谢手把手的教学,还有个小问题,怎么改下能够传递参数。。。
提前定义的函数
aaa: function () {
let iii ;
iii = iii +1 ;
console.log('需要先执行的步骤1')
resolve()
在主程序调用
aaa().then(()=>{
怎么在这里调用到iii
console.log('后续要执行的步骤2')
async function demo () {
let res1 = await aaa() //代码一
代码二
看下这种方法呢,代码二的执行是要根据aaa函数返回的结果来判断吗? 这一块我也不是很熟,平常用的少了
async go_area: function (event) {
let res1 = await aaa(){
代码2。。。
return 123;
代码1在aaa里面,想先执行代码1再执行代码2,是这样的吗?有告警,编译不过去。。。
VM4958:1 thirdScriptError
sdk uncaught third Error
Unexpected token :
SyntaxError: Unexpected token :
console.error @ VM4958:1
(anonymous) @ WAService.js:1
e @ appservice?t=1552813892761:1538
window.onerror @ VM4958:1
module "pages/xx_location/xx_location.js" is not defined
Error: module "pages/xx_location/xx_location.js" is not defined
at require (http://127.0.0.1:22339/appservice/__dev__/WAService.js:1:716408)
at <anonymous>:23:7
at HTMLScriptElement.scriptLoaded (http://127.0.0.1:22339/appservice/appservice?t=1552813892761:1676:21)
at HTMLScriptElement.script.onload (http://127.0.0.1:22339/appservice/appservice?t=1552813892761:1688:20)
scriptLoaded @ appservice?t=1552813892761:1676
script.onload @ appservice?t=1552813892761:1688
WAService.js:1 Sun Mar 17 2019 17:11:33 GMT+0800 (中国标准时间) Page route 错误
VM4958:1 Page[pages/index/index] not found. May be caused by: 1. Forgot to add page route in app.json. 2. Invoking Page() in async task.
console.warn @ VM4958:1
A @ WAService.js:1
Ct @ WAService.js:1
Et @ WAService.js:1
Tt @ WAService.js:1
dt.emit @ WAService.js:1
emit @ WAService.js:1
n.registerCallback.t @ appservice?t=1552813892761:1538
l.forEach.t @ appservice?t=1552813892761:1538
(anonymous) @ appservice?t=1552813892761:1538
s.onmessage @ appservice?t=1552813892761:1538
VM5022:4 Sun Mar 17 2019 17:11:33 GMT+0800 (中国标准时间) appservice 生成错误
VM5022:5 file: pages/xx_location/xx_location.js
unknown: Unexpected token, expected ( (29:15)
27 |
28 |
> 29 | async go_area: function (event) {
| ^
30 | var that = this;
31 | const db = wx.cloud.database()
32 | const _ = db.command
改成这样试试 async go_area(event) {
....
我把代码二用setTimeout延时一点来执行也能达到目的。。。但总感觉不太稳妥,万一网络有延时,代码一没执行完代码二就执行了就不好了。。。
确实会有这种情况,刚才那种方式没有用吗?
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
代码1处理完了,再resolve
具体怎么弄呢?半路出家,异步和同步还没弄明白。。。
aaa: function (equal) {
return new Promise((resolve, reject) => {
代码1
resolve(res)
})
}
编译提示 res is not defined
app.js? [sm]:401 Uncaught (in promise) ReferenceError: res is not defined
at app.js? [sm]:401
at Promise (<anonymous>)
at e.xx_city_getname (app.js? [sm]:344)
at e.<anonymous> (WAService.js:1)
at index.js? [sm]:215
at <anonymous>
你自己写的res,我又不知是啥
非常感谢手把手的教学,还有个小问题,怎么改下能够传递参数。。。
提前定义的函数
aaa: function () {
let iii ;
return new Promise((resolve, reject) => {
iii = iii +1 ;
console.log('需要先执行的步骤1')
resolve()
})
}
在主程序调用
aaa().then(()=>{
怎么在这里调用到iii
console.log('后续要执行的步骤2')
})
async function demo () {
let res1 = await aaa() //代码一
代码二
}
看下这种方法呢,代码二的执行是要根据aaa函数返回的结果来判断吗? 这一块我也不是很熟,平常用的少了
async go_area: function (event) {
let res1 = await aaa(){
代码2。。。
}
return 123;
},
代码1在aaa里面,想先执行代码1再执行代码2,是这样的吗?有告警,编译不过去。。。
VM4958:1 thirdScriptError
sdk uncaught third Error
Unexpected token :
SyntaxError: Unexpected token :
console.error @ VM4958:1
(anonymous) @ WAService.js:1
(anonymous) @ WAService.js:1
e @ appservice?t=1552813892761:1538
window.onerror @ VM4958:1
VM4958:1 thirdScriptError
sdk uncaught third Error
module "pages/xx_location/xx_location.js" is not defined
Error: module "pages/xx_location/xx_location.js" is not defined
at require (http://127.0.0.1:22339/appservice/__dev__/WAService.js:1:716408)
at <anonymous>:23:7
at HTMLScriptElement.scriptLoaded (http://127.0.0.1:22339/appservice/appservice?t=1552813892761:1676:21)
at HTMLScriptElement.script.onload (http://127.0.0.1:22339/appservice/appservice?t=1552813892761:1688:20)
console.error @ VM4958:1
(anonymous) @ WAService.js:1
(anonymous) @ WAService.js:1
e @ appservice?t=1552813892761:1538
window.onerror @ VM4958:1
scriptLoaded @ appservice?t=1552813892761:1676
script.onload @ appservice?t=1552813892761:1688
WAService.js:1 Sun Mar 17 2019 17:11:33 GMT+0800 (中国标准时间) Page route 错误
VM4958:1 Page[pages/index/index] not found. May be caused by: 1. Forgot to add page route in app.json. 2. Invoking Page() in async task.
console.warn @ VM4958:1
A @ WAService.js:1
Ct @ WAService.js:1
Et @ WAService.js:1
Tt @ WAService.js:1
(anonymous) @ WAService.js:1
(anonymous) @ WAService.js:1
dt.emit @ WAService.js:1
emit @ WAService.js:1
(anonymous) @ WAService.js:1
(anonymous) @ WAService.js:1
e @ appservice?t=1552813892761:1538
n.registerCallback.t @ appservice?t=1552813892761:1538
l.forEach.t @ appservice?t=1552813892761:1538
(anonymous) @ appservice?t=1552813892761:1538
s.onmessage @ appservice?t=1552813892761:1538
VM5022:4 Sun Mar 17 2019 17:11:33 GMT+0800 (中国标准时间) appservice 生成错误
VM5022:5 file: pages/xx_location/xx_location.js
unknown: Unexpected token, expected ( (29:15)
27 |
28 |
> 29 | async go_area: function (event) {
| ^
30 | var that = this;
31 | const db = wx.cloud.database()
32 | const _ = db.command
改成这样试试 async go_area(event) {
....
}
我把代码二用setTimeout延时一点来执行也能达到目的。。。但总感觉不太稳妥,万一网络有延时,代码一没执行完代码二就执行了就不好了。。。
确实会有这种情况,刚才那种方式没有用吗?