为什么云函数入口执行不了setTimeout

发布于 6 年前作者 pwu15757 次浏览最后编辑 6 年前来自 ask

// 云函数入口函数

exports.main = async (event, context) => {

console.log(‘Start—>A’);

setTimeout(function () {

console.log(‘Time out’);

}, 100);

console.log(‘Start—>End’);

}

2 回复
weiren
weiren1 楼6 年前

谢谢你,大神~~~

ewan
ewan2 楼4 年前

写法有误 setTimeout是异步的

你需要改成同步的。  Promise

await  new Promise(function (resolve, reject) {

    setTimeout(function () {

      console.log("Time out")

      resolve({

        data: "很好" 

      })

    }, 100)