Tutorial by Examples: callbacks

Given a function that accepts a Node-style callback, fooFn(options, function callback(err, result) { ... }); you can promisify it (convert it to a promise-based function) like this: function promiseFooFn(options) { return new Promise((resolve, reject) => fooFn(options, (err, re...
In functions taking callable as an argument, you can also put a string with PHP built-in function. It's common to use trim as array_map parameter to remove leading and trailing whitespace from all strings in the array. $arr = [' one ', 'two ', ' three']; var_dump(array_map('trim', $arr)); ...
A callback is a method that gets called at specific moments of an object's lifecycle (right before or after creation, deletion, update, validation, saving or loading from the database). For instance, say you have a listing that expires within 30 days of creation. One way to do that is like this: ...
This example wraps the asynchronous method oauth2.client.getToken(callback) from the package NPM package simple-oauth2into a Fiber so that the method may be called synchronously. const oauth2 = require('simple-oauth2')(credentials); const credentials = { clientID: '#####', clientSecret...
Dart has a robust async library, with Future, Stream, and more. However, sometimes you might run into an asynchronous API that uses callbacks instead of Futures. To bridge the gap between callbacks and Futures, Dart offers the Completer class. You can use a Completer to convert a callback into a Fut...
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController include OmniConcern %w[facebook twitter gplus linkedin].each do |meth| define_method(meth) do create end end end Note: In the part “%w[facebook twitter gplus linkedin]”, you should list ...
devise_for :users, controllers: {omniauth_callbacks: 'users/omniauth_callbacks'}
Consider using Extension Methods as Functions which wrap other code, here's a great example that uses both a static method and and extension method to wrap the Try Catch construct. Make your code Bullet Proof... using System; using System.Diagnostics; namespace Samples { /// <summary&g...
There are two types of callbacks associated with committing and rolling back transactions: after_commit and after_rollback. after_commit callbacks are called on every record saved or destroyed within a transaction immediately after the transaction is committed. after_rollback callbacks are called o...
ActionMailer supports three callbacks before_action after_action around_action Provide these in your Mailer class class UserMailer < ApplicationMailer after_action :set_delivery_options, :prevent_delivery_to_guests, :set_business_headers Then create these methods under the private ...
In PubNub JavaScript v3, you could implement a unique callback for every channel that you subscribed to as long as you called the subscribe function for each channel and implemented the callback in that subscribe like this: var pubnub = new PubNub({ publishKey: "your-pub-key", s...
page.js var context = { items: [ {id: 1, name: "Foo"}, {id: 2, name: "Bar"}, {id: 3, name: "Joe"} ] } exports.loaded = function(args){ var page = args.object; page.bindingContext = context; } exports.showEntry = functi...
When you can't / don't want to use Strict Mocks, you can't use MockSequence to validate call order. An alternate approach is to use callbacks to validate that the Setup expectations are being invoked in the expected order. Given the following method to test: public void MethodToTest() { _ut...
Often when using a callback you want access to a specific context. function SomeClass(msg, elem) { this.msg = msg; elem.addEventListener('click', function() { console.log(this.msg); // <= will fail because "this" is undefined }); } var s = new SomeClass("hello&q...
18.0 By default, any function specified in a -callback directive in a behaviour module must be exported by a module that implements that behaviour. Otherwise, you'll get a compiler warning. Sometimes, you want a callback function to be optional: the behaviour would use it if present and exported,...
// Takes a callback and executes it with the read value def readFile(path: String)(callback: Try[String] => Unit): Unit = ??? readFile(path) { _.flatMap { file1 => readFile(path2) { _.foreach { file2 => processFiles(file1, file2) }} }} The function argument to readFile is...
It is very common for C functions to accept pointers to other functions as arguments. Most popular example is setting an action to be executed when a button is clicked in some GUI toolkit library. It is possible to pass Haskell functions as C callbacks. To call this C function: void event_callback...
# all react callbacks are supported using active-record-like syntax class SomeCallBacks < Hyperloop::Component before_mount do # initialize stuff - replaces normal class initialize method end after_mount do # any access to actual generated dom node, or window behaviors goes ...
In the beginning there were callbacks, and callbacks were ok: const getTemperature = (callback) => { http.get('www.temperature.com/current', (res) => { callback(res.data.temperature) }) } const getAirPollution = (callback) => { http.get('www.pollution.com/current', (res) ...

Page 1 of 1