如何在 Promise 中包装函数调用

How to wrap function call inside of Promise

导出所需模块并设置变量后

let AWS = require("aws-sdk");
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');

const USER_POOL_ID = 'us-east-1_vkXRQuP4U';
const CLIENT_ID = 'mipa4trls0l7323om33mlk80e8';

const poolData = { 
  UserPoolId : USER_POOL_ID, ClientId : CLIENT_ID
};
const POOL = new AmazonCognitoIdentity.CognitoUserPool(poolData);

let email = "my.email@domain.com";
let password = "My.Password!";

我可以继续调用 signUp 命令:

POOL.signUp(email, password, [], null, function(err, result) {
  console.log('...result:', result);
});

而且效果很好。接下来我想将 POOL.signUp(email, password...) 包装在 async 函数 sign_up 中,如下所示:

async function sign_up(email, password) {
  POOL.signUp(email, password, [], null, function(err, result) {
    console.log('...sign_up.result:', result);
    return result;
  })
};

async function main() {
  let signupData = await sign_up(email, password);
  console.log('...main.signupData:', signupData);
  return signupData;
};

main().then((error, data) => {console.log('...error, data:', error, data)});

虽然它工作正常,但执行调用的顺序是错误的,因为 main 函数不等待 sign_up() 函数完成。为了纠正这种行为,我将 POOL.signUp(email, password...) 包装在 Promise:

async function sign_up(email, password) {

  return await new Promise((resolve) => {
    POOL.signUp(email, password, [], null, {
      onSuccess: (result) => {
        console.log('...result:', result)
        return resolve(result);
      },      
      onFailure: (err) => {
        return resolve(err.message);
      },
    });
  })
};

但我收到错误消息:

 UnhandledPromiseRejectionWarning: TypeError: callback is not a function

有没有办法避免这个错误?

  • 不需要 await 您返回的 Promise(因为我们恰恰希望我们的函数是异步的,我们希望将等待推迟到调用者)
  • Promise构造函数需要提供第二个reject参数才能在函数实现中访问回调
  • 传递回调函数作为您的 POOL.signUp 第四个参数,而不是对象
function sign_up(email, password) {
  return new Promise((resolve, reject) => {
    POOL.signUp(email, password, [], null, function(err, result) {
      if (err) {
        return reject(err.message);
      }

      console.log('...result:', result)
      resolve(result);
    });
  })
};