JavaScript的:4 asyncronys功能等待对方顺序继续前完成? [英] JavaScript: 4 asyncronys functions to wait for each other sequentially to finish before continuing?
问题描述
我有四个功能:
// NOTE: localDataStore is a function to stringify/parse an item from localStorage
function removeCookie() {
chrome.cookies.remove({"name":"v1guid","url":"http://website.com"},function (cookie){
console.log("cookie removed");
});
}
function getCookie() {
chrome.cookies.get({"url": "http://website.com", "name": "v1guid"}, function(cookie) {
console.log(cookie);
if(cookie !== null) {
console.log("getting cookie");
localDataStore.set("cookie", cookie);
console.log(localDataStore.get("cookie"));
}
});
}
function setCookie() {
var cookiedata = localDataStore.get("cookie");
chrome.cookies.set({
"url": "http://website.com",
"name": cookiedata.name,
"value": cookiedata.value,
"domain": cookiedata.domain,
"path": cookiedata.path,
"secure": cookiedata.secure,
"httpOnly": cookiedata.httpOnly,
"storeId": cookiedata.storeId}, function(cookie) {
console.log("cookietest");
console.log(JSON.stringify(cookie));
});
}
function minePosts() {
// Do a bunch of stuff
}
我试图让他们等待对方的顺序移动到下一个功能之前执行:
I am trying to get them to wait for each other to execute before moving on to the next function in the order:
getCookie();
removeCookie();
minePosts();
setCookie();
我明白这一点可以回调做,但在彼此嵌套4回调使我的code真的很难读,让我的codea的少很多模块化 - 我可能会写我的回拨错了我我不知道。我删除了回调为这个职位,希望有人能帮助我做是正确的。我怎样才能最有效地获得这4功能的顺序运行,并等待对方顺序?我也正在使用jQuery和阅读有关命令$ .Deferred(),但我不知道这是否会在使用它正确的做法。
I understand this can be done with call backs but nesting 4 call backs within each other makes my code really hard to read and makes my code a lot less modular - I may be writing my call backs wrong I am not sure. I removed the callbacks for this post in hopes someone can help me do it correctly. How can I efficiently get these 4 functions to run in that order and wait for each other sequentially? I also am using JQuery and read about the command $.Deferred() but I am not sure if this would be the right practice to use it in.
有人能帮帮我吗?我搜索在互联网,无法找到同样的问题,任何人一样我试图让向上的4种功能等待对方。谢谢!
Can someone please help me? I searched around the internet and couldn't find anyone with the same problem as me trying to get upwards of 4 functions to wait for each other. Thanks!
推荐答案
您可以使用回调或.Deferred $()推迟功能。前一种方法是simplier,而另一方面后者是更强大的,因为你可以使用 deferred.reject()
来处理错误状态。
You can use callbacks or $.Deferred() to defer functions. The former approach is simplier, on the other hand the latter one is more robust, as you can use deferred.reject()
to handle error states.
1)回调
function removeCookie(callback) {
chrome.cookies.remove({"name":"v1guid","url":"http://website.com"},function (cookie){
console.log("cookie removed");
callback();
});
}
等。
getCookie(function(){
removeCookie(function(){
minePosts(function(){
setCookie(function(){
// do something
});
});
});
});
2) $。递延()
2) $.Deferred()
function removeCookie() {
var deferred = $.Deferred();
chrome.cookies.remove({"name":"v1guid","url":"http://website.com"},function (cookie){
console.log("cookie removed");
deferred.resolve();
});
return deferred.promise();
}
等。
getCookie().then(function(){
removeCookie().then(function(){
minePosts().then(function(){
setCookie().then(function(){
// do something
});
});
});
});
或链接由riovel建议(jQuery的1.8 +)
or chained as suggested by riovel (jQuery 1.8+)
getCookie()
.then(removeCookie)
.then(minePosts)
.then(setCookie)
.then(function(){
// do something
});
这篇关于JavaScript的:4 asyncronys功能等待对方顺序继续前完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!