Tag: ecmascript 2017

如何返回来自asynchronous调用的响应?

我有一个函数foo ,这使得一个Ajax请求。 我怎样才能返回foo的回应? 我尝试从successcallback中返回值,并将响应分配给函数内部的局部variables,并返回该variables,但是没有一个方法实际返回响应。 function foo() { var result; $.ajax({ url: '…', success: function(response) { result = response; // return response; // <- I tried that one as well } }); return result; } var result = foo(); // It always ends up being `undefined`.

节点asynchronous/等待 – 我需要尝试/赶上?

我只是玩节点asynchronous/等待,似乎如果你正在等待一个承诺,它被拒绝,它抛出。 我想知道是否有一个更清洁的模式比回到尝试/ catch块,我不知道? async function foo() { const val = await Promise.reject(); }

asynchronous函数永远不会返回

我使用节点版本7.6.0来尝试本地asynchronous并等待function。 我试图找出为什么我的asynchronous调用悬挂从未真正解决。 NLP模块: const rest = require('unirest') const Redis = require('ioredis') const redis = new Redis() const Promise = require('bluebird') const nlp = {} nlp.queryCache = function(text) { return new Promise(function(resolve, reject) { redis.get(text, (err, result) => { if (err) { console.log("Error querying Redis: ", err) reject(new Error("Error querying Redis: ", err)) } else { […]

在es2017中,当从asynchronous方法访问时,“this”是未定义的

如何从asynchronous方法调用时引用类实例。 class Senders { callSendAPI() { //Some code here } sendText() { this.callSendAPI(); } async sendCards() { var dbpromise = await db.call(); console.log('this= ', this); this.callSendAPI(); } } export {Senders}; 这=未定义

在生产入口文件中使用babel-polyfill

我使用babel来转储ES7 js代码,并且所有的东西都像dev / staging中的魅力一样。 在应用程序内部,我非常依赖ES7的asynchronous/等待function。 我的入口文件如下所示: 'use strict'; require("babel-polyfill"); require("babel-core/register"); module.exports = require('./app/server').default(); 我不确定在生产环境中是否有必要保留babel-polyfill和babel-core / register模块,因为我在部署之前使用babel-cli来传输所有内容。 我认为它必须正确工作,即使我删除这些,使入口文件看起来像这样: import server from './app/server'; server(); 但是,如果这样做,我在启动应用程序时遇到了下一个exception: ReferenceError: regeneratorRuntime is not defined at C:\Users\Username\Documents\some-service\lib\app\repositories\someRepository.js:18:32 at Object.<anonymous> (C:\Users\Username\Documents\some-service\lib\app\repositories\someRepository.js:40:2) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17) at Object.<anonymous> (C:\Users\Username\Documents\some-service\lib\app\controllers\someController.js:15:27) at […]

你如何创build一个命名的asynchronous箭头function?

目前,我有这样的代码: async function getConnection(){ // logic here… } 为了使其与我的代码库的其余部分保持一致,我想将其更改为箭头函数。 我试过async getConnection () => { … }但似乎没有工作。 什么是正确的方法来做到这一点?

asynchronous等待node.js跳过第二个等待语句保存mongodb

我正在使用node.js ES6中等待asynchronous… async insertIngot(body, callback) { console.log('*** ItemsRepository.insertIngot'); console.log(body); const data = await this.getItemsTest(); console.log('*** getItems ok'); items = data.items; let item = new Ingot(); item.ingotName = body.ingotName; item.formulaName = body.formulaName; item.items = items; await item.save(); return item; } 第一个等待工作,然后跳过代码的其余部分,并引发错误: (node:16612)DeprecationWarning:Mongoose:mpromise(mongoose的默认承诺库)已被弃用,请插入自己的承诺库: http ://mongoosejs.com/docs/promises.html

节点7.10 – 它支持等待吗?

我在节点7.10上: $ node –version v7.10.0 我认为它支持await 。 let result = await Weather.findOne(options, function(err, weather) { if (err) { res.set('Content-Type', 'application/json'); return res.status(200).send('Error occurs: ' + err); } if (weather) { res.set('Content-Type', 'application/json'); return res.status(200).send(key + ' already exist.'); } return weather; }); console.log(result); 错误信息: let result = await Weather.findOne(options, function(err, weather) { ^^^^^^^ SyntaxError: Unexpected […]

避免在javascript中循环多个返回 – asynchronous/等待解决callback金字塔或callback地狱,

我有这个代码,有很多返回块,例如SignUp() connectors.js const connectors = { Auth: { signUp(args) { return new Promise((resolve, reject) => { // Validate the data if (!args.email) { return reject({ code: 'email.empty', message: 'Email is empty.' }); } else if (!isEmail(args.email)) { return reject({ code: 'email.invalid', message: 'You have to provide a valid email.' }); } if (!args.password) { return […]

正确地使用Promise和Await / Async

我在理解Promisefunction如何工作时遇到了一些问题,我以前使用过Bluebird,但是我想尝试学习新的await / async标准以提高程序员的性能。 我已经使用了asynchronous/等待,并在适当的地方创build了承诺,但是这些function仍然不按顺序执行。 我正在使用Webpack的最新版本的Node上运行这个,我没有得到任何有意义的错误。 它运行得很好,不像预期的那样。 运行它时我的输出是: Searching the Web for: Test String Web search Completed! Promise { <pending> } Response Handler Completed! 理想情况下,我希望它回应: Searching the Web for: Test String Response Handler Completed Web search Completed 然后返回我的响应处理程序的输出。 有谁能发现我的错误? const https = require('https'); // Replace the subscriptionKey string value with your valid subscription key. const subscriptionKey […]