LoginSignup
20
8

More than 3 years have passed since last update.

【備忘録】Lambda(node)でレスポンスを返す方法

Last updated at Posted at 2019-09-24

3通り

async関数でreturn(Promiseを返す)


exports.handler = async (event) => {
    return // レスポンス
}

context.done() | context.succeed() | context.fail()


exports.handler = (event, context) => {
    context.done(null, /*レスポンス*/)
}
// 第一引数に何か入れるとfail

exports.handler = (event, context) => {
    context.succeed(/*レスポンス*/)
}

callback()


exports.handler = (event, context, callback) => {
    callback(null, /*レスポンス*/)
}
// 第一引数に何か入れるとfail

contextとcallbackの違いについて

context.done()などの場合は呼ばれた瞬間にレスポンス返るのに対して、
callback()の場合はLambda内で動いている処理が全て終わるまで待機する。
使い分けができるとかなり便利。

20
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
8