How to solve several questions in javascript telephone interview?

1: there are three interfaces, and the third interface needs to wait for the results returned by the first two interfaces. What should I do?
the difference between 2:let and const, can const only define constants?

Mar.05,2021

first question:
1. Use the fallback function.
2. Use promise.
3. Use async/await.

second problem: the main difference between
let and const is that the value of the const definition variable cannot be changed.
when the value of const is an address pointing to an object, the object can be changed because its address value has not changed.


add the first question. You can also use bad code words on async await, phones and occupy a pit


callback version

a.call(function(){
    b.call(function(){
        c.call(function(){
        });   
    });
});

abc corresponds to interfaces 1, 1, 2, 3, 3

promise version

const aPromise = function() {
    return new Promise((resolve,rejct)=>{
        a.call(e=>e?reject(e):resolve(e));
    });
};

the other two are similar. Call

Promise.all([aPromise,bPromise],function(a,b){
    c.call();
});

async/await version

await aPromise();
await bPromise();
c.call();
  1. objects defined by let can be reassigned, while objects defined by const cannot be reassigned. It is not required to define only constants, as long as the reference remains unchanged.

the most direct thought is promise's all, and
is followed by callback nesting. B clamp is in the callback of A, and C is embedded in the callback of B.

generally speaking, let defines variables, const defines constants
variables defined by let can update values, but not const

in addition, const can define a lot of things, as long as you don't change the body

Menu