Article From:https://segmentfault.com/q/1010000011705130
Question:

For example, how to send A, B and C asynchronously, how to make A, B and C successfully send, then send D

Answer 0:

After all packages are promise, one by one execution.

Answer 1:

Setting mark

var flag = 0;
function checkAndSendD(tag) {
    flag |= tag;
    if (flag === 7) {
        // Send D}}$.post (A, function () {CheckAndSendD (1);})$.post (B, function () {CheckAndSenDD (2);})$.post (C, function () {CheckAndSendD (4);})

Answer 2:

function A(){

succ: D(res)
fail: D(err)

}
B C Similar

function D () {

if count == totalSum 
    Sending D requestsElseCount++

}

Then send request D

Answer 3:

Designed by event driven, inconvenient code for mobile phones.

Answer 4:
Promise.all([
    new Promise(
        (resolve, reject)=>{
            // async A
            setTimeout(()=>resolve(100), 2000)
        }
    ),
    new Promise(
        (resolve, reject)=>{
            // async B
            setTimeout(()=>resolve({a:"this is async B"}), 4000)
        }
    )
]).then(
    (d1, d2)=>console.log(d1,d2)
)

Answer 5:

In view of the uncertainty of which of the three A/B/C requests is asynchronous and which of the final completed requests, it is necessary to declare three flag variables corresponding to three requests and add a condition in the callback function of the three successful requests to call the D event, which is sufficient if all three flag variables are true..

Answer 6:
$.when($.ajax({
    // A...
}), $.ajax({
    // B...
}), $.ajax({
    // C...
})).done(function() {
    $.ajax({
        // D...
    });
}).fail(function() {
    // fail...
});

Answer 7:

Using async syntax, wrapped in a Promise.all, can be implemented as follows:

let getPerData = async () => {
  let p1 = ajax('A')
  let p2 = ajax('B')
  let p3 = ajax('C')
  await Promise.all([p1, p2, p3])
}

getPerData().then(res => {
  ajax('D')
})

Answer 8:

promise It is a way, but when considering that ES6 can be converted first, it can also use counters and so on.

var sends = [];
sendA(() =>{
    sends.push(1);
    ls();
});
sendB(() =>{
    sends.push(1);
    ls();
});
sendC(() =>{
    sends.push(1);
    ls();
});

function ls(){
    if(sends.length >=3){
        sendD();
        sends=[];
    }
}

Answer 9:

es7 If you want to try async/await combination, you can easily implement all kinds of complex nesting problems.

Answer 10:
let p1 = new Promise((resolve) =>{
    ajax().then(function(data1){
        resolve(data1)
    })
})
let p2 = new Promise((resolve) =>{
    ajax().then(function(data2){
        resolve(data2)
    })
})
let p3 = new Promise((resolve) =>{
    ajax().then(function(data3){
        resolve(data3)
    })
})
Promise.all([p1, p2, p3]).then(values => { 
  console.log(values); // [data1, data2, data3] 
});

Answer 11:
        function sleep(second) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve('request done! ' + Math.random());
                }, second);
            })
        }
        async function correctDemo() {
            console.time("Time two ");Let P1 = sleep (1000);Let P2 = sleep (1000);Let P3 = sleeP (1000);Await Promise.all ([p1, P2, p3]);Console.log ('clear the loading~');Console.timeEnd ("time two");/ / output: time two: 1014.670166015625ms}CorRectDemo (); / / clear the loading~

Answer 12:

Promise.all

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *