简体   繁体   English

根据Promise.all中的数据执行其他功能?

[英]Execute different function based on data in Promise.all?

Execute different function in Promise.all 在Promise.all中执行其他功能

Hi I'm new to javascript. 嗨,我是javascript新手。 I need to fetch data from different server which are dependent on each other. 我需要从彼此依赖的不同服务器获取数据。 I want to execute different function depending on promise. 我想根据承诺执行不同的功能。

let us suppose I have fetched user data: 让我们假设我已经获取了用户数据:

 let p1 = SomeApi.fetchUser();
 on successfull I want to execute function A


 let p2 = SomeApi.fetchStudent()
 on successfull I want to execute function B

let p3 = SomeApi.fetchSomething()
on successfull I want to execute function C

 How to achieve some thing like this

 Promise.all([p1, p2, p3]).then(data => {
  if(p1) {
    A()
  }
  if (p2){
    B()
  }
  if (p3) {
    C()
  }
 })

Assume all data are dependent on each other . 假设所有数据相互依赖。

It sounds like you just need to call .then on the existing Promises. 听起来您只需要在现有Promises上调用.then If you need a different action to take place once all Promises have resolved, then use Promise.all : 如果所有承诺都解决后,您需要采取其他措施,请使用Promise.all

let p1 = SomeApi.fetchUser();
p1.then(a);
let p2 = SomeApi.fetchStudent()
p2.then(b);
let p3 = SomeApi.fetchSomething()
p3.then(c);

Promise.all([p1, p2, p3]).then(data => {
  // all Promises have resolved
})

If a , b , and c are completely synchronous and aren't expensive, you have them return the resolve value, and define each outer Promise to the chained call: 如果abc完全同步并且不昂贵,则可以让它们返回resolve值,并将每个外部Promise定义为链式调用:

let p1 = SomeApi.fetchUser()
  .then(a);
let p2 = SomeApi.fetchStudent()
  .then(b);
let p3 = SomeApi.fetchSomething()
  .then(c);

Promise.all([p1, p2, p3]).then(data => {
  // all Promises have resolved
})

where a , b , and c are something like: 其中abc类似于:

const a = (aData) => {
  // do something with aData
  return aData;
};

Returning the data at the end of the individual callback allows the Promise.all to use the resolve values from all Promises (if that's needed). 在各个回调的末尾返回数据使Promise.all可以使用所有 Promises中的resolve值(如果需要)。

If there may be errors, then of course you should handle them as well with catch , eg: 如果可能有错误,那么您当然也应该使用catch来处理它们,例如:

let p1 = SomeApi.fetchUser();
p1.then(a).catch(handleErrors);
let p2 = SomeApi.fetchStudent()
p2.then(b).catch(handleErrors);
let p3 = SomeApi.fetchSomething()
p3.then(c).catch(handleErrors);

Promise.all([p1, p2, p3]).then(data => {
  // all Promises have resolved
}).catch(handleErrors)

You can add .then function to handler for dependent. 您可以将.then函数添加到处理程序以进行依赖。

let p1 = SomeApi.fetchUser().then(()=>{
A();
return Promise.resolve()
});
 //on successfull I want to execute function A


 let p2 = SomeApi.fetchStudent().then(()=>{
B();
return Promise.resolve()
});
 //on successfull I want to execute function B

let p3 = SomeApi.fetchSomething().then(()=>{
C();
return Promise.resolve()
});
//on successfull I want to execute function C

 How to achieve some thing like this

 Promise.all([p1, p2, p3]).then(data => {
  if(data[0]) {

  }
  if (data[1]){

  }
  if (data[2]) {

  }
 });

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

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