本文整理汇总了TypeScript中when.defer函数的典型用法代码示例。如果您正苦于以下问题:TypeScript defer函数的具体用法?TypeScript defer怎么用?TypeScript defer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了defer函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: case3
function case3() {
test.log("");
test.log("Case 3: 'exclude' sessions 2 and 3");
test.log("==================================");
var counter = 0;
var t3 = setInterval(function() {
session1.publish('com.myapp.topic3', [counter], {}, { exclude: [session2.id, session3.id] });
counter += 1;
if (counter > 5) {
session1.publish('com.myapp.topic3', [counter]);
clearInterval(t3);
}
}, delay);
var received2 = 0;
var received3 = 0;
var session2Finished = when.defer();
var session3Finished = when.defer();
var testLog2 = [];
var testLog3 = [];
function onevent2(args) {
received2 += 1;
// testLog2.push("Session 2 got stopper event");
session2Finished.resolve(true);
}
function onevent3(args) {
received3 += 1;
// testLog3.push("Session 3 got stopper event");
session3Finished.resolve(true);
}
when.all([session2Finished.promise, session3Finished.promise]).then(function() {
// clearInterval(t3);
assert(received2 === 1 && received3 === 1, "Case 3: Both clients received final event");
onTestFinished();
});
// both sessions subscribe
session2.subscribe('com.myapp.topic3', onevent2);
session3.subscribe('com.myapp.topic3', onevent3);
}
示例2: longop
function longop(args, kwargs, details) {
test.log("longop()", args, kwargs);
var n = args[0];
var interval_id = null;
if (details.progress) {
var i = 0;
details.progress([i]);
i += 1;
interval_id = setInterval(function() {
if (i < n) {
test.log("longop() - progress", i);
details.progress([i]);
i += 1;
} else {
clearInterval(interval_id);
}
}, 100);
}
var d = when.defer();
setTimeout(function() {
d.resolve(n);
}, 1000 * n);
return d.promise;
}
示例3: file_exists
private static file_exists(filepath:string):Promise {
var fs = require('fs'), def = when.defer()
fs.exists(filepath, (exists)=> {
def.resolve(exists)
})
return def.promise
}
示例4: function
"promise": function () {
var def = when.defer()
setTimeout(function() {
assert(true)
def.resolve()
}, 10)
return def.promise
}
示例5: require
.then((exists)=> {
if (exists)
return when.resolve()
var def = when.defer()
var fs = require('fs')
fs.mkdir(path, (err)=> {
def.resolve()
})
return def.promise
})
示例6: slowsquare
// simulates a "slow" function or a function that
// returns a promise
function slowsquare(x) {
// create a deferred
var d = when.defer();
// resolve the promise after 1s
setTimeout(function() {
d.resolve(x * x);
}, 500);
// need to return the promise
return d.promise;
}
示例7: function
export var http_post = function (url, data, timeout) {
log.debug("new http_post request", url, data, timeout);
var d = when.defer();
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState === 4) {
// Normalize IE's response to HTTP 204 when Win error 1223.
// http://stackoverflow.com/a/10047236/884770
//
var status = (req.status === 1223) ? 204 : req.status;
if (status === 200) {
// response with content
//
d.resolve(req.responseText);
} if (status === 204) {
// empty response
//
d.resolve();
} else {
// anything else is a fail
//
var statusText = null;
try {
statusText = req.statusText;
} catch (e) {
// IE8 fucks up on this
}
d.reject({code: status, text: statusText});
}
}
}
req.open("POST", url, true);
req.setRequestHeader("Content-type", "application/json; charset=utf-8");
if (timeout > 0) {
req.timeout = timeout; // request timeout in ms
req.ontimeout = function () {
d.reject({code: 501, text: "request timeout"});
}
}
if (data) {
req.send(data);
} else {
req.send();
}
if (d.promise.then) {
// whenjs has the actual user promise in an attribute
return d.promise;
} else {
return d;
}
};
示例8: case1
function case1() {
test.log("");
test.log("Case 1: 'eligible' unset");
test.log("===========================");
var counter = 0;
var t1 = setInterval(function() {
session1.publish('com.myapp.topic1', [counter]);
counter += 1;
}, delay);
var received2 = 0;
var received3 = 0;
var session2Finished = when.defer();
var session3Finished = when.defer();
var testLog2 = [];
var testLog3 = [];
function onevent2(args) {
testLog2.push("Session 2 got event: " + args[0]);
received2 += 1;
if (received2 > 5) {
session2Finished.resolve(true);
}
}
function onevent3(args) {
testLog3.push("Session 3 got event: " + args[0]);
received3 += 1;
if (received3 > 5) {
session3Finished.resolve(true);
}
}
when.all([session2Finished.promise, session3Finished.promise]).then(function() {
clearInterval(t1);
assert(true, "Case 1: Both clients received events");
// write the log
var logs = [testLog2, testLog3];
logs.forEach(function(log) {
test.log("-----------")
log.forEach(function(line) {
test.log(line);
})
test.log("----------");
})
case2();
});
// both sessions subscribe
session2.subscribe('com.myapp.topic1', onevent2);
session3.subscribe('com.myapp.topic1', onevent3);
}
示例9: case3
// Case 3: "exclude_me: false"
//
// Expected:
// - session1 (publisher) receives events
// - session2 (other subscriber) receives events
function case3() {
test.log("");
test.log("Case 3: 'exclude_me: false' ");
test.log("===========================");
var counter = 0;
var t3 = setInterval(function() {
session1.publish('com.myapp.topic3', [counter], {}, { exclude_me: false });
counter += 1;
}, delay);
var received1 = 0;
var received2 = 0;
var session1Finished = when.defer();
var session2Finished = when.defer();
var testLog1 = [];
var testLog2 = [];
function onevent1(args) {
testLog1.push("Session 1 got event: " + args[0]);
received1 += 1;
if (received1 > 5) {
session1Finished.resolve(true);
}
}
function onevent2(args) {
testLog2.push("Session 2 got event: " + args[0]);
received2 += 1;
if (received2 > 5) {
session2Finished.resolve(true);
}
}
when.all([session1Finished.promise, session2Finished.promise]).then(function() {
clearInterval(t3);
assert(true, "Case 3: Both clients received events");
// write the log
var logs = [testLog1, testLog2];
logs.forEach(function(log) {
test.log("");
log.forEach(function(line) {
test.log(line);
});
})
onTestFinished();
});
// both sessions subscribe
session1.subscribe('com.myapp.topic3', onevent1);
session2.subscribe('com.myapp.topic3', onevent2);
}