Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*doFirst(function(result) {
  2.     doNext(result, function(newResult) {
  3.       doLast(newResult, function(finalResult) {
  4.         console.log('Got the final result: ' + finalResult);
  5.       }, failureCallback);
  6.     }, failureCallback);
  7.   }, failureCallback);*/
  8.  
  9.   let Q = require('q');
  10.  
  11.   Q.when(null)
  12.   .then(function doFirst(){
  13.       return 'First passes to next';
  14.   })
  15.   .then(function doNext(val){
  16.       console.log(val);
  17.       return Q.when('Last');
  18.   })
  19.   .then(function doLast(val){
  20.       console.log('Here we are at: ' + val);
  21.   })
  22.   .then(function(val){
  23.     console.log(val==undefined);
  24.   });
  25.  
  26. doFirst()
  27. .then(result => doNext(result)
  28. .then(nextResult => doLast(nextResult)
  29. .then(val => console.log(val === undefined));
  30.  
  31. async lifecycleMethod() {
  32.   const val = await doFirst();
  33.   const nextVal = await doNext(val);
  34.   const lastVal = await doLast(nextVal);
  35.   return val === undefined;
  36. }
  37.  
  38. const [val, nextVal, lastVal] = Promise.all([getVal(), getNextVal(), getLastVal()]);
  39.  
  40. getVal() {
  41.   return new Promise(function(resolve,reject){
  42.    const workDone = true; // some time consuming work
  43.       if(workDone){
  44.       //invoke resolve function passed
  45.      
  46.       resolve('success promise completed')
  47.    }
  48.    else{
  49.       reject('ERROR , work could not be completed')
  50.    }
  51.   })
  52. }
  53. ...doNext(val)
  54.  
  55. getVal()
  56. .then(doNext)
  57. .then(doLast)
  58. .then(() => {
  59.   console.log(val === undefined);
  60. });
  61. https://javascript.info/promise-chaining
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement