Tag: async await

使用外部asynchronous

有没有办法让JavaScript的关键字工作以外的asyncfunction? 我希望能够冻结整个调用堆栈(而不仅仅是async函数的其余部分),一旦特定的承诺返回一个值就可以恢复。 可悲的是,像这样的强大的await目前正在实施或尚未实施。 我试图使nodent.js工作,但由于我的自定义加载器和dynamicfunction,这是不幸的。

在循环中使用async / await

如何在for循环中使用async / await? 这是我的代码: export default (req, callback) => { // … compliance.forEach((rule, index) => { let response = await waRuleOverview(req, run.id, rule.id); // handle the response }); } 这是我如何定义waRuleOverview函数: export function waRuleOverview(req, runId, ruleId) { var def = deferred(); setTimeout(function() { const apiToken = req.currentUser.apiToken; const payload = { 'Authorization': 'api_key ' + apiToken […]

asynchronous等待使用和error handling的困境

这两个尝试的打印Promise { <pending> }和第二个有一个Unhandled Promise Rejection Warning 。 我已经成功地使用Promise与.then和.catch,但是还有一些我想使用async / await以更多的同步方式进行编码。 我是否应该用Yield来代替? try { var tokens = getNewTokens('refresh-token', 1) console.log(tokens) } catch (error) { console.log(error.message) } try { tokens = getNewTokens('no-such-refresh-token', 1) console.log(tokens) } catch (error) { console.log(error.message) } function getRefreshToken (refreshToken, userId) { return new Promise((resolve, reject) => { if (refreshToken === 'refresh-token' && […]

asyncawait模块(节点)产生不可用的堆栈跟踪

我在使用node.js( https://github.com/yortus/asyncawait )的async / await实现时遇到问题。 在文档中说“将包括一个可用的堆栈跟踪” 。 但是对我来说并不是这样。 所以我觉得我做错了..但我不知道我做错了什么。 为了演示,我将比较“香草”bluebird示例的stacktrace与相应的asyncawait代码的相应堆栈跟踪(asyncawait在内部使用bluebird,所以我认为这是公平的)。 首先没有asyncawait的蓝鸟代码: var funcB = function() { return Promise.resolve().then(function() { throw new Error("some err"); }) }; var funcA = function() { return funcB(); }; gulp.task("aa",function(cb) { funcA().then(function() { cb(); }); }); 一开始,stacktrace产生信息“funcA – > funcB – > exception”。 没有更好的! Error: Error: some err at processImmediate [as […]

我如何在JavaScript中使用asynchronous生成器?

我有一个API将返回一个游标获取更多的数据。 我嘲笑了这样的: function fetch(n) { return Promise.resolve({ results: [n], next: next < 10 && n + 1, }) } 我想要做的是弄清楚我可以如何使用async / await与发生器一起进行交互。 这基本上是我原型的东西: async function* api(url) { let result = await fetch(url) yield result while (result.next) { result = await fetch(result.next) yield result } } 这个想法是我应该能够创build一个asynchronous生成器,并从该生成器产生,以遍历游标: async function main() { const gen = api(0) const […]

ES6asynchronous/等待在课堂上

我试图创build一个类,将发送一个请求(login),保存cookie和使用该cookie进行其他操作,如下载文件。 我创build了一个本地服务器,它将接收一个带有用户名和密码的post方法,一个名为/download的路由器只在用户login时才被访问,否则会返回you need to log in 。 问题:这是我class的原型(之前): const request = require('request-promise-native') class ImageDownloader { constructor(username = null, password = null) { this.username = username this.password = password this.cookie = request.jar() this.init() } init() { // login and get the cookie } download() { // needs the cookie } query() { // needs the cookie […]

处理102在Node.js中的状态(处理)

我从我的服务器(Node8.9.0LTS)发出一个请求到其他服务,我收到一个102 processing状态代码: const firstRes = await ajaxService.post('/data/search', params) <– issue a request with a timeout –> ctx.data = secondRes.response.data //return the response to the client ajaxService返回一个使用axios发出请求的asynchronous函数。 我如何编写代码发出相同的请求,间隔1秒,限制为5秒(所以我会返回超时到客户端)与asynchronous/等待?

带有promise / async-await的callback上下文

我正在尝试使用ES7的asynchronous等待function,以避免在我的一些代码中的callback地狱。 我正在使用SQLite,我需要在callback的上下文中访问一个variables。 为了说明,这里是从sqlite3 npm模块: module.exports = function() { db.run("INSERT INTO foo …", function(err) { // err is null if insertion was successful console.log("inserted id:", this.lastID); }); }; 假设我创build了一个承诺运行上面的代码,我怎样才能访问this.lastID与asynchronous等待function? module.exports = async function() { try { await db.run("INSERT INTO foo …"); // How can I access the `this` context? } catch (err) { } };

节点repl与asynchronous等待

我想添加对asynchronous/等待到节点repl的支持 在这个问题之后: https : //github.com/nodejs/node/issues/8382 我试图使用这一个https://github.com/paulserraino/babel-repl但它是缺lessasynchronous等待suppport 我想用这个片段 const awaitMatcher = /^(?:\s*(?:(?:let|var|const)\s)?\s*([^=]+)=\s*|^\s*)(await\s[\s\S]*)/; const asyncWrapper = (code, binder) => { let assign = binder ? `root.${binder} = ` : ''; return `(function(){ async function _wrap() { return ${assign}${code} } return _wrap();})()`; }; // match & transform const match = input.match(awaitMatcher); if(match) { input = `${asyncWrapper(match[2], match[1])}`; } […]

尝试在摩卡中使用asynchronous/等待

我想在摩卡中使用asynchronous/等待,以使我的testing。 我已经阅读了很多文章,但是我没有find解决scheme。 我已经安装了所有的babel模块来编译代码,但是它不起作用。 这里是我的代码里面的“testing”文件夹: import test from 'mocha' import 'babel-polyfill' import { expect } from 'chai' import { assert } from 'chai' import utils from '../lib/utils' describe('long number', function () { it("Sample", mochaAsync(async () => { var x = utils.longNums(0); expect(x).to.equal(5000); })) }) 这里是我的package.json,我使用所有的babel依赖和插件,我已经阅读我必须安装,我的testing脚本,我build议摩卡使用巴贝尔transpiling { "name": "pos_lisa-test", "version": "1.0.0", "description": "pos lisa test", "main": "index.js", […]