例えばPromiseをこういうふうに使う
function getfile(url) {
return new Promise(function (res) {
include(~~~);
var tid = setInterval(function () {
if (typeof TEMP_webResBody == 'undefined') return;
clearInterval(tid);
res(TEMP_webResBody); // TEMP_webResBodyを返す
}, 1000);
});
}
で、こうするか
function hoge() {
......
getfile(~~~).then(function (temp) { // tempにTEMP_webResBodyが入る
~インクルード完了後にしたい処理~
});
......
}
もしhoge内でgetfileをただ単に待ちたいならこうする
function co(func) {
return function () {
var gen = func.apply(this, arguments);
function then(rval) {
gen.next(rval).value.then(then);
}
then();
}
}
var hoge = co(function* () {
......
var temp = yield getfile(~~~); // tempにTEMP_webResBodyが入る
~インクルード完了後にしたい処理~
......
});
こっちはジェネレーターが使える環境である必要があるけどよりイメージに近いかな?
5L6L44GI44GwUHJvbWlzZeOCkuOBk+OBhuOBhOOBhuOBteOBhuOBq+S9v+OBhgoKZnVuY3Rpb24gZ2V0ZmlsZSh1cmwpIHsKICAgIHJldHVybiBuZXcgUHJvbWlzZShmdW5jdGlvbiAocmVzKSB7CiAgICAgICAgaW5jbHVkZSjvvZ7vvZ7vvZ4pOwogICAgICAgIHZhciB0aWQgPSBzZXRJbnRlcnZhbChmdW5jdGlvbiAoKSB7CiAgICAgICAgICAgIGlmICh0eXBlb2YgVEVNUF93ZWJSZXNCb2R5ID09ICd1bmRlZmluZWQnKSByZXR1cm47CiAgICAgICAgICAgIGNsZWFySW50ZXJ2YWwodGlkKTsKICAgICAgICAgICAgcmVzKFRFTVBfd2ViUmVzQm9keSk7ICAgIC8vIFRFTVBfd2ViUmVzQm9keeOCkui/lOOBmQogICAgICAgIH0sIDEwMDApOwogICAgfSk7Cn0KCgrjgafjgIHjgZPjgYbjgZnjgovjgYsKCmZ1bmN0aW9uIGhvZ2UoKSB7CiAgICAuLi4uLi4KICAgIGdldGZpbGUo772e772e772eKS50aGVuKGZ1bmN0aW9uICh0ZW1wKSB7ICAgIC8vIHRlbXDjgatURU1QX3dlYlJlc0JvZHnjgYzlhaXjgosKICA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