在Firebase功能中正确链接功能

时间:2019-02-06 02:49:39

标签: javascript node.js firebase firebase-realtime-database google-cloud-functions

我正在Firebase Cloud Functions中构建一个函数,该函数可以利用Node.js模块。

我对使用.then()还是陌生的,我正在努力寻找一种方法来链接我的3个功能webhookSend()emailSendgrid()removeSubmissionProcessor() 'count'递增后立即发生(检查temp_shouldSendWebhook的if语句)。整个返回承诺的想法仍然让我有些困惑,特别是当它涉及外部库时。

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

const request = require('request');

const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
const SENDGRID_API_KEY = firebaseConfig.sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);

exports.submissionProcess = functions.database.ref('/submissions/processor/{submissionId}').onWrite((change, context) => {
  var temp_metaSubmissionCount = 0; // omitted part of function correctly sets the count
  var temp_shouldSendWebhook = true; // omitted part of function correctly sets the boolean

  return admin.database().ref('/submissions/saved/'+'testuser'+'/'+'meta').child('count')
    .set(temp_metaSubmissionCount + 1)
    .then(() => {

      // here is where im stuck
      if (temp_shouldSendWebhook) {
        webhookSend();
        emailSendgrid();
        removeSubmissionProcessor();
      } else {
        emailSendgrid();
        removeSubmissionProcessor();
      }

    })
    .catch(() => {
      console.error("Error updating count")
    });

});

function emailSendgrid() {
  const user = 'test@example.com'
  const name = 'Test name'

  const msg = {
      to: user,
      from: 'hello@angularfirebase.com',
      subject:  'New Follower',
      // text: `Hey ${toName}. You have a new follower!!! `,
      // html: `<strong>Hey ${toName}. You have a new follower!!!</strong>`,

      // custom templates
      templateId: 'your-template-id-1234',
      substitutionWrappers: ['{{', '}}'],
      substitutions: {
        name: name
        // and other custom properties here
      }
  };
  return sgMail.send(msg)
}

function webhookSend() {
  request.post(
    {
      url: 'URLHERE',
      form: {test: "value"}
    },
    function (err, httpResponse, body) {
      console.log('REQUEST RESPONSE', err, body);
    }
  );
}

function removeSubmissionProcessor() {
  admin.database().ref('/submissions/processor').child('submissionkey').remove();
}

我希望能够构造3个函数,使它们被依次调用,以便它们全部执行。

2 个答案:

答案 0 :(得分:3)

为了链接这些功能,它们每个都需要返回一个Promise。当他们这样做时,您可以像这样依次呼叫他们:

return webhookSend()
  .then(() => {
    return emailSendgrid();
  })
  .then(() => {
    return removeSubmissionProcessor();
  });

或者像这样并行:

return Promise.all([webhookSend, emailSendgrid, removeSubmissionProcessor]);

现在,要使您的函数返回承诺:

emailSendgrid:看起来这返回了一个承诺(假设sgMail.send(msg)返回了一个承诺),所以您不需要更改它。

removeSubmissionProcessor:此调用一个函数,该函数返回一个Promise,但不返回该Promise。换句话说,它会触发异步调用(admin.database....remove()),但不等待响应。如果您在通话前添加return,则应该可以。

webhookSend会调用需要回调的函数,因此您要么需要使用fetch(基于Promise),要么不用request,否则您将需要对其进行转换以返回promise以进行链接:

function webhookSend() {
  return new Promise((resolve, reject) => {
    request.post(
      {
        url: 'URLHERE',
        form: {test: "value"}
      },
      function (err, httpResponse, body) {
        console.log('REQUEST RESPONSE', err, body);
        if (err) {
          reject(err);
        } else {
          resolve(body);
        }
      }
    );
  });
}

答案 1 :(得分:1)

使用异步函数,然后可以使用.then()或在每个函数调用之前等待

供参考,请阅读this