async/await

在正式支持 async/await 之前,云函数(不管是否是 async 函数)通过 callback 函数来将执行结果返回给调用方。 而现在 async 函数可以直接使用 return 将结果返回。

info 使用 async + return 时,不要同时使用 callback(除非你对 Node JS 事件循环与 async/await 十分了解), 否则可能产生非预期的结果。

非 async 函数无法使用 return。

云函数执行与结果返回机制,请参考Node.js 事件循环

云函数支持以下两种风格的写法:

  1. 使用 callback

exports.main = function getProduct(event, callback) {
  let tableName = 'product'
  let recordID = '59897882ff650c0477f00485'
  let Product = new BaaS.TableObject(tableName)
  Product.get(recordID).then(product => {
    callback(null, product)
  }).catch(err => {
    callback(err)
  })
}
  1. 使用 async + return

exports.main = async function getProduct(event) {
  let tableName = 'product'
  let recordID = '59897882ff650c0477f00485'
  let Product = new BaaS.TableObject(tableName)
  return await Product.get(recordID)
}

返回错误信息

使用 async + return 风格,云函数返回错误信息的方式与 callback 风格有所不同, 区别如下:

  1. callback 风格

返回值:

  1. async + return 风格

返回值:

info async 函数中单独写 return 或 return undefined,将会被忽略。例如:

Last updated

Was this helpful?