Skip to content

Instantly share code, notes, and snippets.

@g6ling
Last active November 19, 2016 06:08
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/bbc08b8f217d50e72e4873dab056ed3f to your computer and use it in GitHub Desktop.
Save g6ling/bbc08b8f217d50e72e4873dab056ed3f to your computer and use it in GitHub Desktop.
promise chain test 1
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) {
_promise2(true)
.then(function (text) {
console.log(text);
})
console.log(text);
}).then(function() {
console.log('ok3')
});
// result
// ok1 - 3second later
// ok3 - 3second later
// ok2 - 6second later
@voidsatisfaction
Copy link

  1. _promise1 실행(그 안의 async함수들은 async하게 실행된다.)
  2. 그함수의 resolve가 호출되고나서 then쪽을 실행
  3. then쪽의 콜백함수에 promise객체를 반환하지 않으므로 내부의 모든 async함수 실행하고 다음 then으로 이동
  4. 다음 then에서 console.log('ok3')실행
  5. 아까 실행해놨던 _promise2(true)이 함수의 resolve가 호출되고 console.log('ok2')실행

즉, 위의 코드는

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

이거랑 다를바가 없다.

@g6ling
Copy link
Author

g6ling commented Nov 19, 2016

then쪽의 콜백함수에 promise객체를 반환하지 않으므로 내부의 모든 async함수 실행하고 다음 then으로 이동

이게 잘 이해가 안되요

@g6ling
Copy link
Author

g6ling commented Nov 19, 2016

then의 return은 항상 promise객체로 변환되잖아요

@voidsatisfaction
Copy link

여기서 then뒤에는 아에 return 이 없잖아

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