Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript将我的Node6 Firebase函数移动到Node8异步/等待模式_Javascript_Promise_Async Await - Fatal编程技术网

Javascript将我的Node6 Firebase函数移动到Node8异步/等待模式

Javascript将我的Node6 Firebase函数移动到Node8异步/等待模式,javascript,promise,async-await,Javascript,Promise,Async Await,由于firebase函数现在运行w Node8,我想将当前的ES5函数w Promise flow转换为ES6 async/await 我的流程模式如下: const AUTHORIZED = authorizedApi() if AUTHORIZED const SENT = sendContactMessage()) if SENT const FOUND = findContact( if FOUND

由于firebase函数现在运行w Node8,我想将当前的ES5函数w Promise flow转换为ES6 async/await

我的流程模式如下:

const AUTHORIZED = authorizedApi()
  if AUTHORIZED
      const SENT = sendContactMessage())
      if SENT
          const FOUND = findContact(
              if FOUND
                  return "FINISHED"
                  if !FOUND
                      const CREATED = createContact()
                      if CREATED
                          return "FINISHED"
目前我正在使用一个特定的conditionalPromiseFlow()函数,如下所示:(还需要处理错误.)

const conditionalPromiseFlow = (...fns) => {
  if (fns.length === 0) return Promise.resolve();
  const [next] = fns;
  return next().then(result => {
    if (result) {
      return conditionalPromiseFlow(...fns.slice(1));
    }
    return result;
  });
};
我称之为:

conditionalPromiseFlow(
  () => authorizedApi(jwtClient),
  () => sendContactMessage(gmailAPI, encodedContactMessage),
  () =>
    findContact(
      googlePeopleAPI.connections,
      googleConnectionListParams,
      sender.email
    ),
  () => createContact(googlePeopleAPI, googlePeopleContactParams)
)
  .then(
    res => {
      return { status: 200, infos: "done" };
    },
    error => {
      return { status: error.status, infos: error.message };
    }
  )
  .then(response => {
    return res.send(response);
  })
  .catch(console.error);
这运行得很好,但我想async/await模式会简化我的代码……这是真的还是我应该坚持我的当前代码


感谢您的反馈

假设
异步
函数中不包含此项,则
异步
/
等待
等效项为:

(async() => {
  try {
    await authorizedApi(jwtClient);
    await sendContactMessage(gmailAPI, encodedContactMessage);
    await findContact(
      googlePeopleAPI.connections,
      googleConnectionListParams,
      sender.email
    );
    await createContact(googlePeopleAPI, googlePeopleContactParams);
    res.send({ status: 200, infos: "done" });
  } catch (error) {
    res.send({ status: error.status, infos: error.message });
  }
))();
这是否更简单,是否值得做出改变,显然取决于你

(从您的代码中,我认为当这些函数返回的承诺被拒绝时,它们提供的对象上有一个
状态
。)

请注意,我没有在最后的
res.send
前后放置
try
/
catch
。我认为它不会抛出,但您确实有一个
catch
处理程序。因此,如果它抛出,您应该将其放回

如果您已经在一个
async
函数中,显然您不需要该
async
包装器:

try {
  await authorizedApi(jwtClient);
  await sendContactMessage(gmailAPI, encodedContactMessage);
  await findContact(
    googlePeopleAPI.connections,
    googleConnectionListParams,
    sender.email
  );
  await createContact(googlePeopleAPI, googlePeopleContactParams);
  res.send({ status: 200, infos: "done" });
} catch (error) {
  res.send({ status: error.status, infos: error.message });
}
通过
res.send
似乎您正在使用express framework-因此您可以将处理程序设置为异步包装器,在
(req,res)
之前放置
async
字就足够了:


请注意,在上面和第一个代码块中的
async
包装中,整个正文都在
try
中(除了
res.send
on error)。这是因为没有任何内容会处理
async
函数的承诺(Express不处理路由回调的返回值),因此promise不拒绝非常重要。

非常感谢!我希望现在能更好地理解它。。。。(您的权利,我使用的是express framework…当用户发送联系人请求时,会请求此HTTP Firebase函数…它会触发一些Google API函数…gmail将消息发送给管理员,然后检查联系人是否已存在于Firebase DB中,如果没有创建它…@erwin-我没有,也可以说应该向您指出,which是一个新的框架,它来自于与Express相同的人,它在框架级别集成了
async
功能。
app.get('/something', async (req, res) => {
  try {
    /*
    await stuff here
    */
    res.send({ status: 200, infos: "done" });
  } catch (error) {
    res.send({ status: error.status, infos: error.message });
  }
});