Created
          February 27, 2018 02:52 
        
      - 
      
- 
        Save ohtangza/f3cd86ee113b89be8019a5bb09132b2f to your computer and use it in GitHub Desktop. 
    Bluebird.Promise cancellation
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import Promise from 'bluebird'; | |
| Promise.config({ | |
| cancellation: true, | |
| }); | |
| console.log('Experiment A!!!'); | |
| const rootPromise1 = new Promise((resolve, reject, onCancel) => { | |
| console.log('A: rootPromise1 started'); | |
| setTimeout(() => { | |
| resolve(); | |
| }, 100); | |
| onCancel(() => { | |
| console.log('A: rootPromise1 cancelled'); | |
| }); | |
| return Promise.resolve() | |
| .then(() => new Promise((resolve, reject, onCancel) => { | |
| console.log('A: subPromise1 started'); | |
| setTimeout(() => { | |
| resolve(); | |
| }, 100); | |
| onCancel(() => { | |
| console.log('A: subPromise1 cancelled'); | |
| }); | |
| })) | |
| .then(() => new Promise((resolve, reject, onCancel) => { | |
| console.log('A: subPromise2 started'); | |
| setTimeout(() => { | |
| resolve(); | |
| }, 100); | |
| onCancel(() => { | |
| console.log('A: subPromise2 cancelled'); | |
| }); | |
| })); | |
| }); | |
| rootPromise1.cancel(); | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import Promise from 'bluebird'; | |
| Promise.config({ | |
| cancellation: true, | |
| }); | |
| console.log('Experiment B!!!'); | |
| const rootPromise2 = Promise.resolve() | |
| .then(() => new Promise((resolve, reject, onCancel) => { | |
| console.log('B: subPromise1 started'); | |
| setTimeout(() => { | |
| resolve(); | |
| }, 100); | |
| onCancel(() => { | |
| console.log('B: subPromise1 cancelled'); | |
| }); | |
| })) | |
| .then(() => new Promise((resolve, reject, onCancel) => { | |
| console.log('B: subPromise2 started'); | |
| setTimeout(() => { | |
| resolve(); | |
| }, 100); | |
| onCancel(() => { | |
| console.log('B: subPromise2 cancelled'); | |
| }); | |
| })); | |
| setTimeout(() => { | |
| rootPromise2.cancel(); | |
| }, 150); | 
      
      
  Author
  
  
        
      
            ohtangza
  
      
      
      commented 
        Feb 27, 2018 
      
    
  

  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment