2017-04-14 79 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

我試過這個錯誤:回調不是函數 –