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
I recently deployed and tested VMware Horizon, an...
This article shares the installation tutorial of ...
Load balancing is a commonly used device in serve...
Table of contents Overview What are callbacks or ...
Preface Vue (pronounced /vjuː/, similar to view) ...
Table of contents Preface toDoList just do it Pre...
Installation of MySQL decompression version and N...
1. pc-reset PC style initialization /* normalize....
Aggregate functions Acts on a set of data and ret...
Table of contents 1. Three modes of binlog 1.Stat...
Table of contents 1. Don’t treat objects as Maps ...
introduction I discovered a problem before: somet...
This article shares with you the detailed install...
Table of contents Special characters in URLs URL ...
When inserting a set of data into the MySQL datab...