Panduan Referensi APIAWS SDK for JavaScript V3 menjelaskan secara rinci semua operasi API untukAWS SDK for JavaScript versi 3 (V3). Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris. Setiap metode objek layanan dapat menerima fungsi callback anonim sebagai parameter terakhir. Tanda tangan dari fungsi callback ini adalah sebagai berikut.
Fungsi callback ini mengeksekusi ketika baik respon sukses atau data kesalahan kembali. Jika metode panggilan berhasil, isi respon yang tersedia untuk fungsi callback di Biasanya kode di dalam fungsi callback tes untuk kesalahan, yang diproses jika salah satu dikembalikan. Jika kesalahan tidak dikembalikan, kode kemudian mengambil data dalam respon dari
Pada contoh sebelumnya, rincian kesalahan atau data yang dikembalikan dicatat ke konsol. Berikut adalah contoh yang menunjukkan fungsi callback dilewatkan sebagai bagian dari memanggil metode pada objek layanan. Back-end developers run into challenges all the time while building applications or testing code. As a developer who is fairly new and getting acquainted with those challenges, I have never run into a challenge or inconvenience more frequently — or more memorable — than with callback functions. I am not going to delve too deeply into the details of callback and its pros and cons or alternatives like promises and async/await. For a more vivid explanation, you can check out this article which explains them thoroughly. Callback HellCallbacks are a useful feature of JavaScript’s that enables it make asynchronous calls. They are functions that are usually passed on as a second parameter to another function that is fetching data or doing an I/O operation that takes time to complete. For example, try making an API call using the You’d have to nest these calls inside each other: request.get(url, function(error, response, mongoUrl) { if(error) throw new Error("Error while fetching fetching data"); MongoClient.connect(mongoUrl, function(error, client) { if(error) throw new Error("MongoDB connection error"); console.log("Connected successfully to server"); Okay…so where’s the problem? Well, for one thing, the readability of the code suffers from this technique. It may seem OK at first when the codebase is small. But this doesn’t scale well, especially if you go more layers deeper into the nested callbacks. You will end up with a lot of closing brackets and curly braces that will confuse you and other developers no matter how neatly formatted your code is. There is a website called callbackhell that addresses this specific issue. I hear some of you, including my naïve past self, telling me wrap it in an If there is any code block after the the function that uses callbacks, that code block will execute and will NOT wait for the callback. Here’s that mistake that I did before: var request = require('request');// WRONGasync function(){ let joke; Some more experienced devs might say “Just use a different library that uses promises to do the same thing, like axios, or just use fetch”. Sure I can in that scenario, but that’s just running away from the problem. Besides, this is just an example. Sometimes you can be locked into using a library that doesn’t support promises with no alternatives. Like using software development kits (SDKs) to communicate with platforms like Amazon Web Services (AWS), Twitter, or Facebook. Sometimes, even using a callback to do a very simple call with a quick I/O or CRUD operation is fine, and no other logic depends on its results. But you might be constrained by the runtime environment like in a Lambda function which would kill all process once the main thread finishes, regardless of any asynchronous calls that did not complete. Solution 1 (easy): Use Node’s “util” moduleThe solution is surprisingly simple. Even if you’re a little uncomfortable with the idea of promises in JavaScript, you will love how you can solve this issue using them. As pointed out by Erop and Robin in the comments, Nodejs version 8 and above now support turning callback functions into promises using the built-in util module. const request = require('request');const util = require('util'); The above code solves the problem neatly using the method available from nodejs core library. All you have to do is use the callback function as an argument to util.promisify, and store it an a variable. In my case, that’s getChuckNorrisFact. Sometimes, using the request and util libraries is just not possible, whether it’s because of a legacy environment/code base or doing the requests from the client-side browser, you have to wrap a promise around your callback function. Let’s take the Chuck Norris example above, and turn that into a promise. var request = require('request');works like magic In the code above, I put the callback-based var request = require('request');// WRONGasync function(){ let joke;0 and var request = require('request');// WRONGasync function(){ let joke;1 methods. When the var request = require('request');// WRONGasync function(){ let joke;2 is called, it executes the request to the API and waits for either a var request = require('request');// WRONGasync function(){ let joke;3 or a var request = require('request');// WRONGasync function(){ let joke;4 statement to execute. In the callback function, you simply pass the retrieved data into the resolve or reject methods. Once the data (in this case, an awesome Chuck Norris fact) is fetched and passed to the resolver, the var request = require('request');// WRONGasync function(){ let joke;6 method. This will return the result that you can use inside a function inside the var request = require('request');// WRONGasync function(){ let joke;7 to do your desired logic — in this case displaying it to the console. |