相关疑难解决方法(0)

如何在顶层使用async/await?

我一直在浏览async/await,经过几篇文章后,我决定自己测试一下.但是,我似乎无法理解为什么这不起作用:

async function main() {  
    var value = await Promise.resolve('Hey there');
    console.log('inside: ' + value);
    return value;
}

var text = main();  
console.log('outside: ' + text);
Run Code Online (Sandbox Code Playgroud)

控制台输出以下内容(节点v8.6.0):

>外面:[对象承诺]

>里面:嘿那里

为什么函数内部的日志消息会在之后执行?我认为创建async/await的原因是为了使用异步任务执行同步执行.

有没有办法可以使用函数内部返回的值而不使用asyncafter await

javascript node.js async-await ecmascript-2017

117
推荐指数
9
解决办法
4万
查看次数

在新的Promise()构造函数中使用async/await是一种反模式吗?

我正在使用该async.eachLimit功能一次控制最大操作数.

const { eachLimit } = require("async");

function myFunction() {
 return new Promise(async (resolve, reject) => {
   eachLimit((await getAsyncArray), 500, (item, callback) => {
     // do other things that use native promises.
   }, (error) => {
     if (error) return reject(error);
     // resolve here passing the next value.
   });
 });
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我无法将该myFunction函数声明为异步,因为我无法访问eachLimit函数的第二个回调内的值.

javascript asynchronous node.js async-await

62
推荐指数
3
解决办法
3万
查看次数

在异步函数之外使用await

我试图将两个异步函数链接在一起,因为第一个具有条件返回参数,导致第二个参数运行或退出模块.但是,我发现在规格中找不到奇怪的行为.

async function isInLobby() {
    //promise.all([chained methods here])
    let exit = false;
    if (someCondition) exit = true;
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码的一个卑鄙的片段(你可以在这里看到完整的范围),它只是检查一个玩家是否已经在大厅,但这是无关紧要的.

接下来我们有这个异步功能.

async function countPlayer() {
    const keyLength = await scardAsync(game);
    return keyLength;
}
Run Code Online (Sandbox Code Playgroud)

如果这个函数不需要运行exit === true.

我试着这样做

const inLobby = await isInLobby();
Run Code Online (Sandbox Code Playgroud)

我希望这等待结果,所以我可以inLobby用来有条件地运行countPlayer,但是我收到了一个没有具体细节的类型错误.

为什么你不能awaitasync函数范围之外的函数?我知道这是一个糖的承诺,所以它必须被束缚,then但为什么countPlayer我可以等待另一个承诺,但在外面,我不能await isInLobby

javascript node.js async-await

53
推荐指数
4
解决办法
6万
查看次数

为什么TypeScript编译器会忽略tsconfig.json?

我有这个文件,从教程中粘贴(让我们面对它,文档,tuts和示例之间的差异令人震惊):

/scripts/tsconfig.json:

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "commonjs",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../wwwroot/appScripts/",
        "removeComments": false,
        "sourceMap": true,
        "target": "es5",
        "moduleResolution": "node"
    },
    "exclude": [
        "node_modules",
        "typings/index",
        "typings/index.d.ts"
    ]
}
Run Code Online (Sandbox Code Playgroud)

选项设置为在保存时编译,但每当我保存一个TypeScript文件时,JavaScript输出最终在源文件的"下"或"附加到":

TypeScript
|
--test.ts 
    |
    --test.js
Run Code Online (Sandbox Code Playgroud)

,它与源物理位于同一目录中/TypeScript.如果tsconfig.json缺少,编译器会抱怨,但是当它存在时,肯定是,编译器会忽略该"outDir": "../wwwroot/appScripts/"设置.

我真的是Gulp的新手,但Gulp的任务看起来还不错:

var tsProject = ts.createProject('scripts/tsconfig.json');
gulp.task('ts', function (done) {
    //var tsResult = tsProject.src()
    var tsResult = gulp.src([
            "scripts/*.ts"
    ])
        .pipe(ts(tsProject), undefined, ts.reporter.fullReporter());
    return tsResult.js.pipe(gulp.dest('./wwwroot/appScripts'));
});
Run Code Online (Sandbox Code Playgroud)

json typescript gulp asp.net-core

11
推荐指数
2
解决办法
4208
查看次数