How to convert a callback code to promise in ES6

This question already has an answer here: How do I convert an existing callback API to promises? 17 answers The existing answers fall prey to the deferred anti-pattern. I would avoid this approach as it is unnecessarily verbose and doesn't take advantage of the full Promise API. Another of the answers uses promisification. It's really only necessary to use promisification when you

如何将回调代码转换为ES6中的承诺

这个问题在这里已经有了答案: 如何将现有的回调API转换为承诺? 17个答案 现有的答案是延迟反模式的牺牲品。 我会避免使用这种方法,因为它不必要地冗长,并且不利用完整的Promise API。 另一个答案使用promisification。 当你无法修改使用回调风格编写的代码时(例如使用第三方脚本),只需要使用promisification。 你提出了两个问题,其次是为什么Promises按照你在给定例子中看到的方式行事。 为了找到这个问题的

HOW to implement this method with a promise

This question already has an answer here: How do I convert an existing callback API to promises? 17 answers In the browser world you live in now, how you create your own promise unfortunately depends upon which promise library you are using. Here are a couple ways you could create a geocode(address) function that returns its result via a promise: // jQuery promises function geocode(address

如何实现这个承诺的方法

这个问题在这里已经有了答案: 如何将现有的回调API转换为承诺? 17个答案 在您现在居住的浏览器世界中,不幸的是,您如何创建自己的承诺取决于您使用的承诺库。 这里有几种方法可以创建一个通过承诺返回结果的geocode(address)函数: // jQuery promises function geocode(address) { var geocoder = new google.maps.Geocoder(); var def = $.Deferred(); geocoder.geocode({ 'address': value }, function

Angular2: Returning Promise

This question already has an answer here: How do I convert an existing callback API to promises? 17 answers The AWS SDK for JavaScript has supported promises since version 2.3.0 released March 31st. Here's the annoucement. I suggest you update to the latest SDK version if you are using something earlier than 2.3.0, and then use promises instead of callbacks. If you are using TypeScr

Angular2:回归诺言

这个问题在这里已经有了答案: 如何将现有的回调API转换为承诺? 17个答案 AWS SDK for JavaScript从3月31日发布2.3.0版本开始支持许诺。 这是通告。 如果您使用2.3.0之前的版本,我建议您更新到最新的SDK版本,然后使用promises而不是回调。 如果你使用TypeScript,你可以做这样的事情来返回一个promise 。 我可以想象它的JavaScript风格大致相同: getBuckets() { return new Promise(function (resolve, reje

AngularJS promise not resolving file with FileReader

This question already has an answer here: How do I convert an existing callback API to promises? 17 answers 您没有onload事件,因此它实际上不会返回读取的文件数据。 this.fileRead = function(file) { var deferred = $q.defer(); var reader = new FileReader(); reader.onload = function() { deferred.resolve(reader.result); }; reader.readAsDataURL(file); return defe

AngularJS承诺不使用FileReader解析文件

这个问题在这里已经有了答案: 如何将现有的回调API转换为承诺? 17个答案 您没有onload事件,因此它实际上不会返回读取的文件数据。 this.fileRead = function(file) { var deferred = $q.defer(); var reader = new FileReader(); reader.onload = function() { deferred.resolve(reader.result); }; reader.readAsDataURL(file); return deferred.promise; };

How to return a promise when writestream finishes?

This question already has an answer here: How do I convert an existing callback API to promises? 17 answers 你会想使用Promise构造函数: function writeToFile(filePath: string, arr: string[]): Promise<boolean> { return new Promise((resolve, reject) => { const file = fs.createWriteStream(filePath); for (const row of arr) { file.write(row + "n"); } file.end();

writestream完成时如何返回承诺?

这个问题在这里已经有了答案: 如何将现有的回调API转换为承诺? 17个答案 你会想使用Promise构造函数: function writeToFile(filePath: string, arr: string[]): Promise<boolean> { return new Promise((resolve, reject) => { const file = fs.createWriteStream(filePath); for (const row of arr) { file.write(row + "n"); } file.end(); file.on("finish", () => { resolve(

How to Promisify this function

This question already has an answer here: How do I convert an existing callback API to promises? 17 answers You have the error because create() is not a Promise. Promisifying an async function is quite easy (nodejs has a built-in Promise support nowadays): function createTicket(ticket) { // 1 - Create a new Promise return new Promise(function (resolve, reject) { // 2 - Copy

如何提升这个功能

这个问题在这里已经有了答案: 如何将现有的回调API转换为承诺? 17个答案 你有错误,因为create()不是一个Promise。 宣布一个异步函数非常简单(nodejs现在有一个内置的Promise支持): function createTicket(ticket) { // 1 - Create a new Promise return new Promise(function (resolve, reject) { // 2 - Copy-paste your code inside this function client.tickets.create(ticket, functio

How to make a promise from setTimeout

This question already has an answer here: How do I convert an existing callback API to promises? 17 answers Update (2017) Here in 2017, Promises are built into JavaScript, they were added by the ES2015 spec (polyfills are available for outdated environments like IE8-IE11). The syntax they went with uses a callback you pass into the Promise constructor (the Promise executor) which receives

如何从setTimeout做出承诺

这个问题在这里已经有了答案: 如何将现有的回调API转换为承诺? 17个答案 更新(2017) 在2017年,Promises被嵌入到JavaScript中,它们被ES2015规范添加(polyfills可用于过时的环境,如IE8-IE11)。 它们的语法使用一个回调函数传递给Promise构造函数( Promise executor),该函数接收解析/拒绝promise的函数作为参数。 首先,由于async现在在JavaScript中有意义(即使它只是某些上下文中的关键字),我将later用作

Chaining promises with promises inside then()

How do you chain in an scenario like this? api is a function that returns a promise after an http request. auth is a function that returns a promise after api respond, if resolves api is called for a second time, if not auth rejects. I tried this, but not only am I going back to the callback hell, it does not work. function api(query) { return new Promise(function(resolve, reject) {

()内的承诺链接承诺

你如何链接这样的场景? api是一个在http请求后返回promise的函数。 auth是一个函数,它在api响应后返回一个promise,如果解析api被第二次调用,如果不是auth拒绝。 我试过这个,但我不仅回到了回调地狱,它不起作用。 function api(query) { return new Promise(function(resolve, reject) { //DO SOME STUFF AND SOMETIMES resolves... }) } function auth() { return new Promise(function(resolve, reje

Chaining Javascript promises

I'm trying to understand Promises from the MDN documentation. The first example demonstrates the then and catch methods: // We define what to do when the promise is resolved/fulfilled with the then() call, // and the catch() method defines what to do if the promise is rejected. p1.then( // Log the fulfillment value function(val) { log.insertAdjacentHTML('beforeend', val +

链接Javascript的承诺

我试图从MDN文档中了解Promises。 第一个例子演示了then和catch方法: // We define what to do when the promise is resolved/fulfilled with the then() call, // and the catch() method defines what to do if the promise is rejected. p1.then( // Log the fulfillment value function(val) { log.insertAdjacentHTML('beforeend', val + ') Promise fulfilled (<small>Async code ter

Confuse about error and reject in Promise

All: I am pretty new to JS Promise, there is one confuse when it comes to Promise chaining, say I have a promise chaining like: var p = new Promise(function(res, rej){ }) .then( function(data){ }, function(err){ }) .then( function(data){ }, function(err){ }) .catch( function(err){ }) What confuse me: When the function(err) get called and when the ca

在Promise中混淆错误并拒绝

所有: 我对JS Promise非常陌生,当谈到Promise链接时有一种混淆,说我有一个承诺链接如下: var p = new Promise(function(res, rej){ }) .then( function(data){ }, function(err){ }) .then( function(data){ }, function(err){ }) .catch( function(err){ }) 什么使我困惑: 当函数(err)被调用并且catch被调用时? 如何解决并拒绝then ? 谢谢 使用Promise的公式是