我需要創建一個函數來加載Windows通用JavaScript應用程序中的文本文件,該文件返回的字符串不是「承諾」。UWP JavaScript加載本地文本文件,沒有「異步調用」
這段代碼將返回一個「promise」而不是一個字符串,所以有一種方法可以將它嵌入到一個函數中(它將等待並返回一個字符串),或者完成其他加載文件的方式。
function getFileContentAsync(fileName) {
var fileName = new Windows.Foundation.Uri("ms-appx:///" + fileName);
return Windows.Storage.StorageFile.getFileFromApplicationUriAsync(fileName).then(function (file) {
return Windows.Storage.FileIO.readTextAsync(file);
});
});
//usage
getFileContentAsync(filename).then(function(fileContent){
...
});
我需要一個函數,它將接收一個fileName並返回一個String;
可能重複[如何從異步調用返回響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an - 異步調用) –