Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:4个asyncronys函数在继续之前依次等待对方完成?_Javascript_Jquery_Asynchronous_Google Chrome Extension_Callback - Fatal编程技术网

JavaScript:4个asyncronys函数在继续之前依次等待对方完成?

JavaScript:4个asyncronys函数在继续之前依次等待对方完成?,javascript,jquery,asynchronous,google-chrome-extension,callback,Javascript,Jquery,Asynchronous,Google Chrome Extension,Callback,我有四个功能: // 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"); }); } func

我有四个功能:

// 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
}
我试图让它们在继续执行下一个函数之前等待彼此执行:

getCookie();
removeCookie();
minePosts();
setCookie();
我知道这可以通过回调来实现,但是相互嵌套的4个回调使得我的代码很难阅读,并且使我的代码模块化程度大大降低——我可能写错了回调,我不确定。我删除了这个帖子的回调,希望有人能帮助我正确地完成它。我如何才能有效地让这4个函数按顺序运行并依次等待对方?我还使用JQuery并阅读了有关命令$.Deferred()的内容,但我不确定这是否是在中使用它的正确做法


有人能帮我吗?我在互联网上搜索,找不到任何人有和我一样的问题,我试图获得4个以上的函数来互相等待。谢谢

您可以使用回调或$.Deferred()来延迟函数。前一种方法更简单,另一方面后一种方法更健壮,因为您可以使用
deferred.reject()
来处理错误状态

1)回调

function removeCookie(callback) {
    chrome.cookies.remove({"name":"v1guid","url":"http://website.com"},function (cookie){
        console.log("cookie removed");
        callback();
    });
}
等等

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();
}
等等

或者按照riovel(jQuery 1.8+)的建议进行链接


这里有一个。

如果异步函数返回承诺,则只能使用
$.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
            });
        });
    });
});
getCookie()
    .then(removeCookie)
    .then(minePosts)
    .then(setCookie)
    .then(function(){
        // do something
    });
function one() {
  console.log('one');

  deferred = jQuery.Deferred();

  setTimeout(function() {
    console.log('one done');
    deferred.resolve();
  }, 1000);

  return deferred;
}

function two() {
  console.log('two');

  deferred = jQuery.Deferred();

  setTimeout(function() {
    console.log('two done');
    deferred.resolve();
  }, 1000);

  return deferred;
}

function three() {
  console.log('three');

  deferred = jQuery.Deferred();

  setTimeout(function() {
    console.log('three done');
    deferred.resolve();
  }, 1000);

  return deferred;
}

function four() {
  console.log('four');

  deferred = jQuery.Deferred();

  setTimeout(function() {
    console.log('four done');
    deferred.resolve();
  }, 1000);

  return deferred;
}

one()
.then(two)
.then(three)
.then(four);