2017-04-14 95 views
0

嘿,伙计们,这是形势火灾完成

function makeIt() 
{ 
    //code 
    createSomething() 
} 

function createSomething(){ 
//code 
    request.execute(function(resp) { 
    function writeSomething(){ 
    //code 
    } 
    createSomething() 
    goback() 
} 
} 

我想writesomething完成后执行GoBack的。 问题是,writesomething函数可以在2秒或10秒内完成(取决于文件)。我现在使用setTimeout来确保。 但是在完成写入操作时,我怎么能让goback执行?

编辑:

function writeToSheet(){ 
//this is a part of the function (deleted some information) 
       params 

      var xhr = new XMLHttpRequest(); 

      xhr.open('PUT', 'https://something.com'); 
      xhr.setRequestHeader(some things); 
      xhr.send(JSON.stringify(params)); 
     } 

回答

3

见更新,现在你已经包括了writeToSheet定义。


如果writeSomething异步过程中,它会为你知道当它完成  —它会接受一个回调,返回一个承诺,等传递goback作为回调的方式(或作为承诺上的then回调等)。

示例 - 如果writeSomething接受回调:这取决于你是否要goback

writeSomething(other, arguments, here, goback); 
// This is the callback ---------------^^^^^^ 

writeSomething(other, arguments, here, function() { 
    goback(); 
}); 

...接收writeSomething通过其回调争论什么。

示例 - 如果writeSomething回报承诺:

writeSomething(other, arguments, here).then(goback); 

writeSomething(other, arguments, here).then(function() { 
    goback(); 
}); 

...这也取决于你是否想goback接收传递给then回调的价值。


如果writeSomething同步过程可能需要2-10秒,只需拨打goback()调用writeSomething后。即使writeSomething需要10秒钟,如果它真的是同步的,goback将不会被调用,直到它完成。

例(只是为了完整性:-)):

writeSomething(other, arguments, here); 
goback(); 

更新:您writeToSheet函数启动一个异步的过程,所以我们要编辑它要么接受一个回调或返回诺言。

接受回调:如果不

function writeToSheet(callback){ 
//     ^------------------------------------ *** 
    var xhr = new XMLHttpRequest(); 
    xhr.onreadystatechange = function() {    // *** 
     if (xhr.readyState === 4) {      // *** 
      callback(xhr.status === 200);    // *** 
     }            // *** 
    }; 
    xhr.open('PUT', 'https://something.com'); 
    xhr.setRequestHeader(some things); 
    xhr.send(JSON.stringify(params)); 
} 

writeToSheet将调用回调与true如果它是成功的,false

然后:

writeToSheet(goback); 

writeToSheet(function(flag) { 
    // Maybe use the flag here, or not, depends on what you want 
    goback(); 
}); 

...如果你不想goback接收标志。

返回一个承诺:

function writeToSheet(){ 
    return new Promise(function(resolve, reject) {   // *** 
     var xhr = new XMLHttpRequest(); 
     xhr.onreadystatechange = function() {    // *** 
      if (xhr.readyState === 4) {      // *** 
       if (xhr.status === 200) {     // *** 
        resolve();        // *** 
       } else {          // *** 
        reject();        // *** 
       }           // *** 
      }            // *** 
     }; 
     xhr.open('PUT', 'https://something.com'); 
     xhr.setRequestHeader(some things); 
     xhr.send(JSON.stringify(params)); 
    }); 
} 

则:

writeToSheet().then(goback).catch(function() { 
    // It failed 
}); 

...这只会调用goback成功和呼叫失败的其他功能,或

writeToSheet().then(goback, goback); 

无论如何,这将会拨打goback

+0

@Mralwayswrong:在最后添加更新。 –

+0

为什么我得到一个错误:回调不是一个函数? –

+0

@Mralwayswrong:与上述,你不会。确保你传入一个函数作为参数'callback'对应的参数。 –

0

底部传递回调函数作为参数

function writeSomething(callback) { 
    ... 
    callback(); 
} 

function myCallbackFunction() { 
    alert('hi!'); 
} 

writeSomething(myCallbackFunction); 
+0

代码转储不是有用的答案。 –

+0

我试过这个错误:回调不是函数 –