1. What is Promise?
2. Why is there Promise?Promise was created to solve several problems with the callback mechanism used in asynchronous programming:
Callback hell: Promise can turn nested callbacks into .then().then()…, making code writing and reading more intuitive
Three Promise common APIs
Four Promise common usages
.then() is a function that does not return a value, which will cause the Promise chain to no longer continue. At this time, calling .then() later will have no effect. Promise.resolve('foo').then(function(s) { console.log(s); }).then(function(s) { // Never executed console.log(s); }); There is a return value function in .then(), which allows the Promise chain to continue Promise.resolve('foo').then(function(s) { console.log(s); return s + 'bar'; }).then(function(s) { console.log(s); }); // foo // foobar .then() has a function that returns a value and the return value is another Promise object, which will also make the Promise continue. The difference from the former is that calling .then() again may trigger an asynchronous operation, so the next round of resolve() is not triggered immediately. Promise.resolve('foo').then(function(s) { return new Promise((resolve, reject) => { console.log(s); setTimeout(() => { resolve(s + 'bar') }, 1000); }); }).then(function(s) { console.log(s); }); // foo // foobar (displayed 1 second after "foo" is displayed)
//demo const promise1 = Promise.resolve(3); const promise2 = 42; const promise3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, 'foo'); }); Promise.all([promise1, promise2, promise3]).then((values) => { console.log(values); }); // expected output: Array [3, 42, "foo"] Difference between Promise.all() and sync await //sync await operation time 2 seconds async function Index2() { console.time() const p1 = await new Promise((resolve, reject) => { console.log('Here is p1') setTimeout(() => { resolve('Here is the return of p1') }, 1000) }) const p2 = await new Promise((resolve, reject) => { console.log('Here is p2') setTimeout(() => { resolve('Here is the return of p2') }, 1000) }) console.log(p1) console.log(p2) console.timeEnd() } Index2(); // Use Promise.all() to implement the call. Operation time 1 second function Index() { console.time() const p1 = new Promise((resolve, reject) => { console.log('Here is p1') setTimeout(() => { resolve('Here is the return of p1') }, 1000) }) const p2 = new Promise((resolve, reject) => { console.log('Here is p2') setTimeout(() => { resolve('Here is the return of p2') }, 1000) }) Promise.all([p1, p2]).then((val) => { console.log(val) console.timeEnd() }) } SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: A designer complains about Hammer's official website again
>>: Basic knowledge of MySQL database
<br />In order to clearly distinguish the ta...
1. Background Netplan is a new command-line netwo...
It is very simple to build a go environment under...
Table of contents 1. Problem 2. Solution 2.1 Pagi...
The root account of mysql, I usually use localhos...
Table of contents Introduction Instructions Actua...
CSS3 Patterns Gallery This CSS3 pattern library s...
1. Qualitative changes brought by CSS variables T...
There was an article about the execution process ...
The docker exec command can execute commands in a...
1. Use curl command to access by default: # curl ...
Table of contents 1. Self-enumerable properties 2...
The operating environment of this tutorial: Windo...
Preface A Docker image consists of a Dockerfile a...
I've been asked a lot lately about an apparen...