LoginSignup
0

More than 3 years have passed since last update.

Promise Test

Last updated at Posted at 2020-07-11
function sampleResolve(value) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(value);
        }, 1000);
    })
}

function sample() {
    let result = 0;

    return sampleResolve(5)
        .then(val => {
            console.log(result, val)
            result += val;
            return sampleResolve(10);
        })
        .then(val => {
            console.log(result, val)
            result *= val;
            return sampleResolve(20);
        })
        .then(val => {
            console.log(result, val)
            result += val;
            return result;
        });
}

sample().then((v) => {
    console.log(v); // => 70
});
0 5
5 10
50 20
70

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0