Skip to content

Instantly share code, notes, and snippets.

@g6ling
Created November 17, 2016 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g6ling/ecf2a15cd1afa8ab10047274bc4c914d to your computer and use it in GitHub Desktop.
Save g6ling/ecf2a15cd1afa8ab10047274bc4c914d to your computer and use it in GitHub Desktop.
promise chain test 2
var _promise1 = function (param) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("ok1");
}, 3000);
});
};
var _promise2 = function (param) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve("ok2");
}, 3000);
});
};
_promise1(true)
.then(function (text) {
console.log(text);
return _promise2(true)
}).then(function (text) {
console.log(text);
}).then(function() {
console.log('ok3')
});
// result
// ok1 - 3second later
// ok2 - 6second later
// ok3 - 6second later
@voidsatisfaction
Copy link

voidsatisfaction commented Nov 19, 2016

  1. _promise1실행
  2. 그 함수의 resolve를 실행하고 나서 then쪽을 진행
  3. return 에서 _promise2(true)를 실행하여 promise 객체를 넘겨줌
  4. 넘겨준 프로미스 객체가 resolve되면 다음then을 진행함
  5. 그 then 은 resolve의 인수를 받아서 console.log로 출력
  6. 그뒤에 다음 then을 실행해서 console.log('ok3')출력

즉 위의 코드는

_promise1(true)
.then(function (text) {
	console.log(text);
	return _promise2(true)
}).then(function (text) {
	console.log(text);
	console.log('ok3')
});

와 다를바가 없다

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