SOURCE

if(!Promise.observe){
  Promise.observe = function(pr,cb){
    pr.then(
    	function fulfilled(msg){
        Promise.resolve(msg).then(cb);
      },
      function rejected(err){
        Promise.resolve(err).then(cb);
      }
    );
    return pr;
  }
}

function foo(delay){
  return new Promise(function(resolve,reject){
    setTimeout(function(){
      resolve("foo!");
    },delay);
  });
}


function timeoutPromise(delay){
  return new Promise(function(resolve,reject){
    setTimeout(function(){
      reject("Timeout!");
    },delay);
  });
}

Promise.race([
  Promise.observe(
  	foo(1000),
    function cleanup(msg){
      console.log(msg+"abc");
    }
  ),
  timeoutPromise(3000)
]).then(
	function fulfilled(msg){
    console.log("continue"+msg);
  },
  function rejected(err){
    console.log("error"+err);
  }
)
console 命令行工具 X clear

                    
>
console