Skip to content

Instantly share code, notes, and snippets.

@hax hax/index.html
Created Feb 2, 2015

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// See https://github.com/promises-aplus/promises-spec/issues/179
'use strict'
var logs = []
function log(s) {
return function () {
logs.push(s)
}
}
setTimeout(function () { console.log(logs.join(' ')) }, 100)
var r1, r2
var a = new Promise(function (resolve) { r1 = resolve })
var b = new Promise(function (resolve) { r2 = resolve })
var p0 = {
_f: [],
then: function (onFulfilled, onRejected) {
log('then')()
if (this._fulfilled) onFulfilled()
else this._f.push(onFulfilled)
}
}
function f0() {
p0._fulfilled = true
p0._f.forEach(function (f) { f() })
}
a.then(log('a1'))
b.then(log('b1'))
b.then(log('b2'))
a.then(log('a2'))
r1(p0)
r2(p0)
a.then(log('a3'))
b.then(log('b3'))
b.then(log('b4'))
a.then(log('a4'))
f0()
a.then(log('a5'))
b.then(log('b5'))
b.then(log('b6'))
a.then(log('a6'))
setTimeout(function () {
log('timeout')()
a.then(log('a7'))
b.then(log('b7'))
b.then(log('b8'))
a.then(log('a8'))
})
log('async')()
</script>
<script id="jsbin-source-javascript" type="text/javascript">// See https://github.com/promises-aplus/promises-spec/issues/179
'use strict'
var logs = []
function log(s) {
return function () {
logs.push(s)
}
}
setTimeout(function () { console.log(logs.join(' ')) }, 100)
var r1, r2
var a = new Promise(function (resolve) { r1 = resolve })
var b = new Promise(function (resolve) { r2 = resolve })
var p0 = {
_f: [],
then: function (onFulfilled, onRejected) {
log('then')()
if (this._fulfilled) onFulfilled()
else this._f.push(onFulfilled)
}
}
function f0() {
p0._fulfilled = true
p0._f.forEach(function (f) { f() })
}
a.then(log('a1'))
b.then(log('b1'))
b.then(log('b2'))
a.then(log('a2'))
r1(p0)
r2(p0)
a.then(log('a3'))
b.then(log('b3'))
b.then(log('b4'))
a.then(log('a4'))
f0()
a.then(log('a5'))
b.then(log('b5'))
b.then(log('b6'))
a.then(log('a6'))
setTimeout(function () {
log('timeout')()
a.then(log('a7'))
b.then(log('b7'))
b.then(log('b8'))
a.then(log('a8'))
})
log('async')()
</script></body>
</html>
// See https://github.com/promises-aplus/promises-spec/issues/179
'use strict'
var logs = []
function log(s) {
return function () {
logs.push(s)
}
}
setTimeout(function () { console.log(logs.join(' ')) }, 100)
var r1, r2
var a = new Promise(function (resolve) { r1 = resolve })
var b = new Promise(function (resolve) { r2 = resolve })
var p0 = {
_f: [],
then: function (onFulfilled, onRejected) {
log('then')()
if (this._fulfilled) onFulfilled()
else this._f.push(onFulfilled)
}
}
function f0() {
p0._fulfilled = true
p0._f.forEach(function (f) { f() })
}
a.then(log('a1'))
b.then(log('b1'))
b.then(log('b2'))
a.then(log('a2'))
r1(p0)
r2(p0)
a.then(log('a3'))
b.then(log('b3'))
b.then(log('b4'))
a.then(log('a4'))
f0()
a.then(log('a5'))
b.then(log('b5'))
b.then(log('b6'))
a.then(log('a6'))
setTimeout(function () {
log('timeout')()
a.then(log('a7'))
b.then(log('b7'))
b.then(log('b8'))
a.then(log('a8'))
})
log('async')()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.