Last active
March 17, 2016 11:15
-
-
Save gaogao-9/d3376f155e37a87ab5dd to your computer and use it in GitHub Desktop.
async/await等で頻繁に利用する待機可能なPromiseパターンを予めパッケージ化してしまえば便利かなーと思いましたので、色々作っておきました。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncSleep from "./asyncSleep.js"; | |
function asyncDOMContentLoaded(target=window, timeout=10000){ | |
if(target.document.readyState !== "loading"){ | |
return Promise.resolve({ target, data: null }); | |
} | |
return Promise.race([ | |
asyncSleep(timeout).then(Promise.reject(new Error("asyncDOMContentLoaded is timeout"))), | |
new Promise((resolve, reject)=>{ | |
target.document.addEventListener("DOMContentLoaded", (event)=>{ | |
resolve({ target, data: event }); | |
}, false); | |
}) | |
]); | |
} | |
export default asyncDOMContentLoaded; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncSleep from "./asyncSleep.js"; | |
function asyncImage(image, timeout=10000){ | |
if(!(image instanceof HTMLImageElement)){ | |
return Promise.reject(new TypeError("first arguments of asyncImage must be HTMLImageElement.")); | |
} | |
if((image.src !== "") && image.complete){ | |
return Promise.resolve({ target: image, data: null }); | |
} | |
return Promise.race([ | |
asyncSleep(timeout).then(Promise.reject(new Error("asyncImage is timeout"))), | |
new Promise((resolve, reject)=>{ | |
image.addEventListener("load", (event)=>{ | |
resolve({ target:image, data: event }); | |
}, false); | |
image.addEventListener("error", (event)=>{ | |
reject(event); | |
}, false); | |
}) | |
]); | |
} | |
export defualt asyncImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncSleep from "./asyncSleep.js"; | |
function asyncLoad(target=window, timeout=10000){ | |
if(target.document.readyState === "complete"){ | |
return Promise.resolve({ target, data: null }); | |
} | |
return Promise.race([ | |
asyncSleep(timeout).then(Promise.reject(new Error("asyncLoad is timeout"))), | |
new Promise((resolve, reject)=>{ | |
target.document.addEventListener("load", (event)=>{ | |
resolve({ target, data: event }); | |
}, false); | |
}) | |
]); | |
} | |
export default asyncLoad; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import readline from "readline"; | |
function asyncQuestion(text, input=process.stdin, output=process.stdout){ | |
if(typeof(text) !== "string"){ | |
return Promise.reject(new TypeError("first arguments of asyncQuestion must be String.")); | |
} | |
return new Promise((resolve,reject)=>{ | |
const rli = readline.createInterface(input, output) | |
rli.question(text, (answer)=>{ | |
resolve({target: text, data: answer}); | |
rli.close(); | |
}); | |
rli.on("close", ()=>{ | |
reject(new Error("asyncQuestion is interrupted.")); | |
}); | |
}); | |
} | |
export default asyncQuestion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const maxDelay = -1 >>> 1; | |
// setTimeoutはsigned 32bitまでしか対応してないので、それ以上の遅延は分割await実装で対応する | |
async function asyncSleep(delay){ | |
if(!Number.isSafeInteger(delay)){ | |
throw new TypeError("first arguments of asyncSleep must be SafeInteger"); | |
} | |
if(delay<0){ | |
throw new TypeError("first arguments of asyncSleep must be >=0"); | |
} | |
while(delay>maxDelay){ | |
await sleep32bit(maxDelay); | |
delay -= maxDelay; | |
} | |
await sleep32bit(delay); | |
return { target: null, data: null }; | |
} | |
function sleep32bit(ms){ | |
return new Promise(resolve=>{ | |
setTimeout(resolve,ms); | |
}); | |
} | |
export default asyncSleep; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment