Skip to content

Instantly share code, notes, and snippets.

@bluemoon2014
Last active April 24, 2018 09:41
Show Gist options
  • Save bluemoon2014/40c4d2973e1988354a83d313bcf804fb to your computer and use it in GitHub Desktop.
Save bluemoon2014/40c4d2973e1988354a83d313bcf804fb to your computer and use it in GitHub Desktop.
许诺的学习
//then 中 是可以把返回值 当做promise 包装的
//但是在new promise 中 是没有这个能力的 return 只会导致跳出函数 promise 的状态永远不会被设定
//无论return 与否 链式的then 都会一直往下走 找下一个then
//catch 后 如果什么都没有 也会一直依次的走then 下去
//return function 直接不执行了 就等你调用 函数的参数 只能写名字 不能写一个函数实现了 最多写函数变量名
//return promise 则提供一个函数作为参数 这个参数不是外面给的 而是自己用的
//resolve 后 还是会执行promise里的语句的
//一旦resolve 或者reject 后 promise 状态就不再变化了
//promise 里的代码实际上是在调用promise时候同步执行的 一直执行到resolve 或是reject 以后,
//也还是会继续同步执行,一直到末尾或者遇到return 除非有代码是异步的 不然里面的代码实际上是同步的
//只不过遇到resolve 或者是reject 后才把结果调用转为异步的
//不知为何 resolve reject 是可以作为函数似的参数传递的 我在别出传递函数参数就不可以
//
//
//对于promise chain 来说 是回调来执行的 没有错 只不过 如果return 一个promise chain
//的话,那么,这个promisechain 要全部执行完到末尾 末尾return的才是真正的return
//所以return 许诺链的话 实际上就是异步的return 直到这个链全部异步执行完了 再 执行return->❎ 是立即返回
//感觉是不是把周围的参数都保存起来了呢??
//
//promise chain 的话 return 其实是真的立即就return 一个promise 但是 然而 这个promise的状态
//是要到promise chain 最后走完了才会变的 不然一直是pendding嗯嗯 就是这样 哈哈..
//cancel后 promise 是会退出链的 但是也得执行完自己的then c或者catch 语句后退出
//在链中 不返回promise 默认就是resolve的! then会一直练下去
//异步调用时候 好像调用的函数栈都保存着 所以 实际上 参数都会保存下来
//估计和java的不一样吧 nodejs是运行的只有一个线程 但是异步有很多回调顺序
//java 估计是每个请求分配一个线程,在请求流中是一个线程,但是请求流之间就是多线程了?
var Promise = require("bluebird");
var fs = require("fs");
var test = require("./文件内调用.js");
console.log("out0");
function test1(argument) {
return new Promise(function(resolve, reject) {
console.log("test1 fun in ");
// reject([3,3,4,3]);
// setTimeout(resolve,2000,argument);
resolve(999);
// return 9;
})
}
console.log("out1");
function start(argument) {
return new Promise(function(resolve, reject) {
console.log("start");
// test1().then(function(argument) {
// console.log("li" + argument);
// return 90;
// });
// resolve(88);
console.log("start2");
// reject(111);
resolve(444);
// test1().then(resolve,reject);
// return new Promise(function (resolve, reject) {
// console.log("start3");
// resolve(777);
});
// reject(77);
// return 9;
}
// console.log("out2");
// start().then(function(argument) {
// console.log("then1 jj " + argument);
// return test1();
// throw "k";
// // return 8;
// }).catch(function(argument) {
// console.log("catch1 " +argument );
// // console.log(argument);
// // console.log("catch1 " );
// }).then(function(argument) {
// console.log("then2 " + argument);
// }).catch(function(argument) {
// console.log("catch2");
// })
// function lll(function (argument) {
// console.log("1");
// }) {
// console.log("0");
// }
// function jjj(argument) {
// console.log("0");
// return function (argument) {
// console.log("1");
// function (argument) {
// console.log("2");
// }
// }
// }
// jjj()()(); //->0 1 2
function hi(argument) {
// if (argument) { }
console.log("hi");
setTimeout(hi,2000);
}
function hello(argument) {
// body...
// argument("hello argument");
// hi(argument);
objc.j = 2;
}
var ff = function (argument) {
// body...
console.log(argument);
}
var objc = {j:1};
// hello(objc);
// console.log(objc);
// ---------------------------------------------
// Promise.reduce(
// Iterable<any>|Promise<Iterable<any>> input,
// function(any accumulator, any item, int index, int length) reducer,
// [any initialValue]
// ) -> Promise
// Promise.reduce(["file1.txt", "file2.txt", "file3.txt"], function(total, fileName, index, length) {
// console.log("----");
// console.log(total);
// console.log(fileName);
// console.log(index);
// console.log(length);
// console.log("------");
// return test1(fileName).then(function(argument) {
// console.log("test1 then " + argument);
// return start();
// });
// }, 0).then(function(total) {
// console.log(total);
// }).catch(function (argument) {
// console.log(argument);
// })
// setTimeout(function (argument) {
// console.log("time out");
// },2000);
// test.fa("jj");
// test1().then(aa => {console.log(aa);});//can't
// test1().then(function (argument) {
// // body...
// return test1().then(function (argument) {
// // body...
// console.log(2);
// return test1();
// }).then(function (argument) {
// // body...
// console.log(3);
// return test1(9898);
// });
// }).then(function (argument) {
// // body...
// console.log(4);
// })
//
//
// out0
// out1
// test1 fun in
// end
// test1 fun in
// 2
// test1 fun in
// 3
// test1 fun in
// 4
function testReject(argument) {
// body...
return Promise.reject();
}
//可以设置让promise 可以取消
Promise.config({
// Enable cancellation
cancellation: true,
});
// global.jj = {j:1};
//可以的哦
// global.jj.c = 2;
// console.log(global.jj);
// var pp2;
// var pp = test1().then(function (argument) {
// console.log(1);
// pp2 = testReject();
// check(pp);
// return pp2;
// }).catch(function(argument) {
// console.log(2);
// check(pp);
// //get out of chain
// // console.log(pp);
// // pp.cancel();
// // console.log("cancel!!!");
// // return test1();
// }).then(function(){
// console.log(3);
// check(pp);
// // return testReject();
// }).then(function(){
// console.log(4);
// check(pp);
// }).catch(function(){
// console.log(5);
// check(pp);
// // return test1();
// }).then(function(){
// console.log(6);
// // console.log(pp2);
// check(pp);
// pp2.catch(function(){
// console.log("oooooo");
// });//当其状态已经改变的时候 注册一个函数后 就在回调里存入了准备回调的准备!
// check(pp2);
// // console.log(pp2);
// }).catch(function(){
// console.log(7);
// }).then(function(){
// console.log(8);
// })
// pp.then(function(){
// console.log("jjjjjj");
// check(pp);
// });
//也就是说 如果哪一步失败了 就去找最近的catch 去处理 如果有的话 然后就继续then的进行
//如果没有加catch 就会 Unhandled rejection undefined
// test1().then(function(){
// return testReject();
// }).then(function(){
// console.log(111111);
// })
//promise all 如果是空数组的话 那么就 默认成功了
//但是别的promise 里 就不会了 不调用状态不会改变的
// new Promise(function (resolve, reject) {
// console.log(0);
// resolve()
// }).then(function(argument) {
// console.log(1);
// }).catch(function (argument) {
// console.log(2);
// });
// Promise.all([]).then(function(argument) {
// console.log(1);
// }).catch(function (argument) {
// console.log(2);
// });
console.log("end");
// console.log(pp.isResolved());
function check(promise) {
console.log(promise.isResolved());
}
// var jjj = [1,2,3].map(function (argument) {
// return test1(argument);
// });
var haha = "hahahhahah!";
// console.log(jjj);
function fff(){
console.log("bbbbegin");
var pp;
var p;
test1().then(scope);
function scope(argument) {
console.log("scope");
console.log(haha);
pp = test1();
if (true) { test1().then(function () {
console.log("if");
});
return;
}
p.then(test1).then(test1).then(testReject).then(function () {
console.log("new");
}).catch(function () {
console.log("new1");
});
console.log("promise now has 1");
p.then(function (argument) {
console.log("2 then");
});
console.log("promise now has 2");
p.catch(function (argument) {
console.log("3 catch");
p.cancel();
})
console.log("promise now has 3");
p.then(function (argument) {
console.log("4 then");
});
p.catch(function () {
console.log("catach 222");
});
p.catch(function () {
console.log("catach 223");
});
console.log("scope end");
}
}
// fff();
// for (var i = 0; i < 1000; i++) {
// console.log("jjjjjjjjj");
// }now
var param = {name:"origin"};
// var param = "origin";
// for (var i = 0; i < 10; i++) {
// hh(param,i);
// // param += i;
// param.name = "" +i;
// }
// var aaa = [1,2,3];
// console.log(...aaa);
function hh(param, name) {
// console.log(...aaa);
return new Promise(function (resolve, reject) {
console.log("out " + JSON.stringify(param));
// setTimeout(function () {
// console.log("sleep");
// resolve();
// },0);
fs.readFile(__dirname +"/package.json",function () {
console.log("file");
});
});
}
// -----promise chain 是怎么状态更改的
//-------promise 怎么不用等待执行完了下一步 直接异步任务自己玩去 证明是可以的...
// test1().then(function () {
// return test1();
// }).then(function () {
// hh("jjjjjjjjjjjjj");
// return Promise.resolve();
// }).then(function () {
// console.log("last");
// })
//----------------调用的函数如果写在外边...
//也就是说 就是单纯的调用一个函数 函数本身已经结束 故事还在原来的promise chain那里!
test1().then(outFunc).then(function () {
console.log("outFunc then");
});
function outFunc() {
console.log("outFunc");
return test1();
}
console.log("eeeeend");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment