current position:Home>[Vue (3), written java test of Neusoft group

[Vue (3), written java test of Neusoft group

2021-08-27 06:06:49 Programmer Jie Jie

 Promise Basic use



new Promise((resolve, reject) => {

  setTimeout(() => {

    resolve("hello world!");

    reject("error data!");

  }, 1000);

}).then(data => {

  console.log(data);

}).catch(error => {

  console.log(error);

}); 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

analysis :resolve and reject Is the function , Usually, which one is called depends on the success and failure of the requested data

  • If it works , Would call resolve(messsage), At this point, subsequent then Will be recalled

  • If you fail , Would call reject(error), At this point, subsequent catch Will be recalled

 Promise Three states


 Insert picture description here

When there are asynchronous operations in development , You can wrap an asynchronous operation with Promise, After an asynchronous operation, there are three states

  • pending: Wait state , For example, a network request is in progress , Or the timer doesn't run out of time .

  • fulfill: State of satisfaction , When we take the initiative to call back resolve In this state when , And it will call back .then()

  • reject: Rejection status , When we take the initiative to call back reject In this state when , And it will call back .catch()

 Promise call chaining


The most complicated way to write :


new Promise((resolve, reject) => {

  setTimeout(() => {

    resolve('aaa')

  }, 1000)

}).then(res => {

  console.log(res, ' First floor 10 Line processing code ');

  return new Promise((resolve, reject) => {

    resolve(res + '111')

  })

}).then(res => {

  console.log(res, ' Second floor 10 Line processing code ');

  return new Promise(resolve => {

    resolve(res + '222')

  })

}).then(res => {

  console.log(res, ' On the third floor 10 Line processing code ');

}).catch(err => {

  console.log(err);

}) 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

Simplify writing , Use Promise.resolve() and Promise.reject()

  • Promise.resovle(): Packaging data as Promise object , And call back internally resolve() function

  • Promise.reject(): Packaging data as Promise object , And call back internally reject() function


new Promise((resolve, reject) => {

  setTimeout(() => {

    resolve('aaa')

  }, 1000)

}).then(res => {

  console.log(res, ' First floor 10 Line processing code ');

  return Promise.resolve(res + '111')

}).then(res => {

  console.log(res, ' Second floor 10 Line processing code ');

  return Promise.resolve(res + '222')

}).then(res => {

  console.log(res, ' On the third floor 10 Line processing code ');

}); 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

Simplify writing : direct return data


new Promise((resolve, reject) => {

  setTimeout(() => {

    resolve('aaa')

  }, 1000)

}).then(res => {

  console.log(res, ' First floor 10 Line processing code ');

  return res + '111'

}).then(res => {

  console.log(res, ' Second floor 10 Line processing code ');

  return res + '222'



###  Last 

** Over time ,  The following is an in-depth understanding of Xiaobian's induction and arrangement Java Virtual machine documentation , I hope I can help you pass the exam and pass the interview smoothly .**
 Because the whole document is comprehensive , More content , Space is not allowed , Here is a screenshot of  . If there is a friend who needs to get information and documents ,[ You can click here for free ](https://gitee.com/vip204888/java-p7)
![](https://s2.51cto.com/images/20210823/1629700594275945.jpg)
![](https://s2.51cto.com/images/20210823/1629700594193968.jpg)
![](https://s2.51cto.com/images/20210823/1629700594961926.jpg)
![](https://s2.51cto.com/images/20210823/1629700594939009.jpg)
![](https://s2.51cto.com/images/20210823/1629700595907677.jpg)
![](https://s2.51cto.com/images/20210823/1629700595730692.jpg)
![](https://s2.51cto.com/images/20210823/1629700595716417.jpg)

** Due to space limitation , The detailed information of the document is too comprehensive , There are too many details , Therefore, only a partial screenshot of the knowledge points is provided for a rough introduction , Each small node has more detailed content !**
     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

copyright notice
author[Programmer Jie Jie],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827060648362w.html

Random recommended