簡體   English   中英

在 node.js 中更正異步 function 導出

[英]Correct async function export in node.js

我的自定義模塊包含以下代碼:

module.exports.PrintNearestStore = async function PrintNearestStore(session, lat, lon) {
...
}

如果在我的模塊外部調用 function 它工作正常,但是如果我在內部調用我在運行時出錯:

(節點:24372)UnhandledPromiseRejectionWarning:未處理的 promise 拒絕(拒絕 ID:1):ReferenceError:未定義 PrintNearestStore

當我將語法更改為:

module.exports.PrintNearestStore = PrintNearestStore;

var PrintNearestStore = async function(session, lat, lon) {

}

它在模塊內部開始正常工作,但在模塊外部失敗 - 我收到錯誤消息:

(節點:32422)UnhandledPromiseRejectionWarning:未處理的 promise 拒絕(拒絕 ID:1):TypeError:mymodule.PrintNearestStore 不是 function

所以我將代碼更改為:

module.exports.PrintNearestStore = async function(session, lat, lon) {
    await PrintNearestStore(session, lat, lon);
}

var PrintNearestStore = async function(session, lat, lon) {
...
}

現在它適用於所有情況:內部和外部。 然而想要理解語義,是否有更漂亮和更短的方式來寫呢? 如何正確定義和使用async function both: inside and outside (exports) 模塊?

這實際上與異步函數沒有任何關系。 如果要在內部調用函數導出它,請先定義它,然后再導出它。

async function doStuff() {
  // ...
}
// doStuff is defined inside the module so we can call it wherever we want

// Export it to make it available outside
module.exports.doStuff = doStuff;

您嘗試的問題的解釋:

module.exports.PrintNearestStore = async function PrintNearestStore(session, lat, lon) {
...
}

這不會在模塊中定義函數。 函數定義是一個函數表達式 函數表達式的名稱僅在函數本身內部創建一個變量。 更簡單的例子:

 var foo = function bar() { console.log(typeof bar); // 'function' - works }; foo(); console.log(typeof foo); // 'function' - works console.log(typeof bar); // 'undefined' - there is no such variable `bar`

另請參見命名函數表達式揭秘 如果您在module.exports.PrintNearestStore地方都引用module.exports.PrintNearestStore您當然可以引用該函數。


module.exports.PrintNearestStore = PrintNearestStore;

var PrintNearestStore = async function(session, lat, lon) {

}

幾乎沒問題 問題是PrintNearestStore的值在您將其分配給module.exports.PrintNearestStoreundefined 執行順序為:

var PrintNearestStore; // `undefined` by default
// still `undefined`, hence `module.exports.PrintNearestStore` is `undefined`
module.exports.PrintNearestStore = PrintNearestStore;

PrintNearestStore = async function(session, lat, lon) {}
// now has a function as value, but it's too late

更簡單的例子:

 var foo = bar; console.log(foo, bar); // logs `undefined`, `undefined` because `bar` is `undefined` var bar = 21; console.log(foo, bar); // logs `undefined`, `21`

如果您更改了順序,它將按預期工作。


module.exports.PrintNearestStore = async function(session, lat, lon) {
    await PrintNearestStore(session, lat, lon);
}

var PrintNearestStore = async function(session, lat, lon) {
...
}

這是有效的,因為執行分配給module.exports.PrintNearestStore的函數時, PrintNearestStore已將該函數作為其值。

更簡單的例子:

 var foo = function() { console.log(bar); }; foo(); // logs `undefined` var bar = 21; foo(); // logs `21`

另一種選擇是像這樣導出。 // foo.js

export async function foo(){ 
 console.log('I am greatest of all.'); // for the person who reads it, just say it.
}

然后在其他腳本中使用它,例如

import { foo } from './foo'

foo();

第一種情況出錯: PrintNearestStore - 函數表達式,因此此名稱在外部不可用。

第二種情況的錯誤:使用變量,而不是函數聲明。 在這種情況下,變量 PrintNearestStore 的聲明被提升,因此,您可以在行var PrintNearestStore = ...之前使用此名稱,但在這種情況下, value 將為undefined

因此,最簡單的解決方案更改第二個變體,如下所示:

module.exports.PrintNearestStore = PrintNearestStore;

async function PrintNearestStore(session, lat, lon) {

}
export let handlePostStore = async (data) => {
    console.log('post');
    return data;
};

// to import 
import { handlePostStore } from 'your_path_here';

// to call it 
handlePostStore(data)

一些例子:

module.exports.func1 = async function func1(id) {  // name is preferred by linter
  //
};

module.exports.func1 = async function (id) { // ok
  //
};

module.exports.func1 = async (id) => { // simpler
  //
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM