代码之家  ›  专栏  ›  技术社区  ›  user762579

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

  •  1
  • user762579  · 技术社区  · 7 年前

    由于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);
    

    谢谢你的反馈

    1 回复  |  直到 7 年前
        1
  •  2
  •   T.J. Crowder    7 年前

    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 });
      }
    ))();
    

    (根据您的代码,我认为当这些函数返回的承诺被拒绝时,它们提供的对象具有 status

    请注意,我没有把 try catch 大约在去年 res.send 抓住 处理程序。所以如果它抛出,你会想把它放回去。

    如果你是 已经 功能,显然你不需要 包装器:

    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) :

    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 });
      }
    });
    

    请注意,在上述和 整个的 尝试 res.send 异步的 函数(Express不处理路由回调的返回值),因此promise不拒绝这一点很重要。