简体   繁体   English

如何创建一个函数,返回一个在内部承诺解决后解决的承诺

[英]How to create a function returning a promise that resolves after inner promise resolves

I am trying to create a wrapper function that returns a Promise object which will resolve after an inner Promise resolves.我正在尝试创建一个包装函数,该函数返回一个 Promise 对象,该对象将在内部 Promise 解析后解析。 Basically, something like this:基本上,是这样的:

function wrapper() {
  return new Promise((resolve, reject) => {
    MyApi.asyncCall()
      .then(data => {
        // do this first, then resolve outer promise
        resolve();
      })
      .catch(e => {
        reject();
      });
  });
}

The reason I need to do this is I have an interface that accepts a promise argument, then triggers an action once that promise resolves.我需要这样做的原因是我有一个接受承诺参数的接口,然后在承诺解决后触发一个动作。 I need to make an async call, then do something with the response, then trigger the interface.我需要进行异步调用,然后对响应执行某些操作,然后触发接口。

Edit Explanation of purpose: I need to perform actions with the response of the inner promise BEFORE resolving the outer promise.编辑目的说明:在解决外部承诺之前,我需要对内部承诺的响应执行操作。 The interface accepts an unresolved promise object, and then mounts a component when it is resolved.该接口接受一个未解析的 promise 对象,然后在解析时挂载一个组件。 If I pass the inner promise it will mount before the actions on the data response are performed.如果我通过内部承诺,它将在对数据响应执行操作之前安装。

Basically — you doing it right.基本上 - 你做对了。 You are resolve \\ reject promise when you need.您在需要时决心\\拒绝承诺。

But you can do it easily — simply return original promise:但是你可以很容易地做到——只需返回原始承诺:

function wrapper() {
  return MyApi.asyncCall()
}

So your returning promise, which resolves or rejectes when MyApi.asyncCall resolves or rejects.所以你的返回承诺,当 MyApi.asyncCall 解决或拒绝时解决或拒绝。

If you need to do some staff inside you can do it inside this promise:如果你需要在里面做一些员工,你可以在这个承诺里面做:

function wrapper() {
  return MyApi.asyncCall()
    .then((data) => {
      doSomeOtherStaff(data)
      return data
    })
}

Note that if you need data outside — you have to return it from your then callback inside.请注意,如果您需要外部数据 - 您必须从内部的 then 回调中返回它。

Can you explain why you need the wrapper promise exactly?你能解释一下为什么你需要包装器承诺吗?

MyApi.asyncCall()
      .then(data => {
        // do this first, then resolve outer promise
        resolve();
      })
      .catch(e => {
        reject();
      });

is a promise itself, and it should be enough to just return it.本身就是一个promise,只要返回它就足够了。

You can use async await for the first promise and after getting data of the first promise then return the second promise :您可以对第一个承诺使用 async await ,并在获取第一个承诺的数据后返回第二个承诺:

function wrapper(){
  return new Promise( async   (resolve,reject)=>{
   let myFirstPromise =  await firestPromise();
   // do your stuff here
   console.log(myFirstPromise)
   return resolve('Second Promise');
  });
}

function firestPromise(){
  return new Promise( (resolve,reject)=>{
   return resolve('First Promise');
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM