比如在 setInterval 或者 setTimeout 里 return new Promise, 然后调用该方法,就会报错,小程序里面会报 Cannot read property ‘then’ of undefined

    1. test: function () {
    2. setTimeout(()=>{
    3. return new Promise((resolve, reject) => {
    4. resolve(666)
    5. })
    6. },3000)
    7. },
    8. onShow: function () {
    9. this.test().then(res =>{
    10. console.log(res)
    11. })
    12. }

    上面这样使用 promise 会报错,应该将定时器放在 Promise 里面,以上代码修改为:

    1. test: function () {
    2. return new Promise((resolve, reject) => {
    3. setTimeout(()=>{
    4. resolve(666)
    5. },3000)
    6. })
    7. },
    8. onShow: function () {
    9. this.test().then(res =>{
    10. console.log(res) // 3秒后会打印出 666
    11. })
    12. }