IonIon
Packages
  • next
  • current
Changelog
GitHub
Packages
  • next
  • current
Changelog
GitHub
  • Version 0.x

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • Packages

    • Introduction
    • Container

      • Introduction
      • Prerequisites
      • How to install
      • Container Instance
      • Bindings
      • Dependencies
      • Resolving
      • Contextual Bindings
    • Contracts

      • Introduction
      • How to install
    • Support

      • Introduction
      • How to install
      • Arrays

        • About Arrays
        • Includes All
        • Includes Any
        • Is Array Like
        • Is Concat Spreadable
        • Is Safe Array Like
        • Is Typed Array
        • Merge
      • Concerns

        • About Concerns
        • Prerequisites
        • Concern Class
        • Using Concerns
        • Aliases
        • Conflict Resolution
        • Booting
        • Hooks
        • Edge Cases
        • JSDoc
      • Exceptions

        • About Exceptions
        • Configure Custom Error
        • Configure Stack Trace
        • Get Error Message
        • Custom Errors
      • Facades

        • About Facades
      • Meta

        • About Meta
        • Prerequisites
        • Supported Elements
        • Set & Get
        • Inheritance
        • Outside Changes
        • TC39 Proposal
        • Target Meta
      • Mixins

        • About Mixins
        • New Mixin
        • Apply Mixins
        • Instanceof
        • Inheritance
        • Onward
      • Object

        • About Objects
        • Forget
        • Forget All
        • Get
        • Has
        • Has All
        • Has Any
        • Has Unique ID
        • Is Cloneable
        • Is Populatable
        • Isset
        • Merge
        • Populate
        • Set
        • Unique ID
      • Reflections

        • About reflections
        • Assert Has Prototype Prop.
        • Class Looks Like
        • Class Own Keys
        • Get All Parents Of Class
        • Get Class Prop. Descriptor
        • Get Class Prop. Descriptors
        • Get Constructor Name
        • Get Name Or Desc. Tag
        • Get Parent Of Class
        • Has All Methods
        • Has Method
        • Has Prototype Property
        • Is Callable
        • Is Class Constructor
        • Is Class Method Reference
        • Is Constructor
        • Is Key Safe
        • Is Key Unsafe
        • Is Method
        • Is Subclass
        • Is Subclass Or Looks Like
        • Is WeakKind
      • Misc

        • About Misc.
        • Desc. Tag
        • Empty
        • Is Key
        • Is Primitive
        • Is Property Key
        • Isset
        • Merge Keys
        • To Weak Ref.
      • Callback Wrapper
    • Vuepress Utils

      • Introduction
      • How to install
      • Navigation

        • Archive
      • Plugins

        • Last Updated
      • Components

        • Version Disclaimer
    • XYZ (test package)

Callback Wrapper Available since v0.11

The CallbackWrapper objects offers a convenient way to wrap a callable function.

import { CallbackWrapper } from "@aedart/support";

const wrapped = CallbackWrapper.make(() => {
    return 'Hi there...';
});

// Later in your application
wrapped.call(); // Hi there...
  • Call
  • Arguments
    • Via make()
    • Via with()
    • Via arguments
  • Binding
    • Binding vs. Arrow Function
  • Misc.
    • Custom Callback Wrapper

Call

The call() method invokes the wrapped callback and returns its eventual output.

const wrapped = CallbackWrapper.make(() => {
    return true;
});

wrapped.call(); // true

Arguments

There are several ways to specify arguments that must be applied for the wrapped callback, when call() is invoked.

Via make()

The static make() method allows you to specify arguments right away. This is useful, if you already know the arguments.

const wrapped = CallbackWrapper.make((firstname, lastname) => {
    return `Hi ${firstname} ${lastname}`;
}, 'Timmy', 'Jackson');

wrapped.call(); // Hi Timmy Jackson

Via with()

In situations when you must add additional arguments, e.g. because you might not know all arguments up front, then you can use the with() method.

const wrapped = CallbackWrapper.make((firstname, lastname) => {
    return `Hi ${firstname} ${lastname}`;
}, 'Siw');

wrapped
    .with('Orion')
    .call(); // Hi Siw Orion

Via arguments

Lastly, in situations when you must completely overwrite all arguments, then you can specify them via the arguments property.

const wrapped = CallbackWrapper.make((firstname, lastname) => {
    return `Hi ${firstname} ${lastname}`;
});

wrapped.arguments = [ 'Alpha', 'Zero' ];
wrapped
    .call(); // Hi Alpha Zero

Binding

Use bind() to specify the callback's this value.

class A {
    sayHi(name) {
        return `Hi ${name}`;
    }
}
const instance = new A();

const wrapped = CallbackWrapper.make(function(name) {
    return this.sayHi(name);
});

wrapped
    .bind(instance)
    .with('Akari')
    .call(); // Hi Akari

Binding vs. Arrow Function

warning

It is not possible to apply a binding on an arrow function callback. Doing so can result in a TypeError or other unexpected behaviour. See Mozilla's documentation for additional information.

❌

// Callback Wrapper for arrow function...
const wrapped = CallbackWrapper.make(() => {
    // ...not shown ...
});

wrapped
    .bind(myObject)
    .call(); // TypeError

✔️

// Callback Wrapper for normal function...
const wrapped = CallbackWrapper.make(function () {
    // ...not shown ...
});

wrapped
    .bind(myObject)
    .call();

Misc.

If you need to determine if a value is a "callback wrapper" object, then you can use the isCallbackWrapper() util.

import { isCallbackWrapper, CallbackWrapper } from "@aedart/support";

isCallbackWrapper(() => true); // false
isCallbackWrapper(CallbackWrapper.make(() => true)); // true

Custom Callback Wrapper

isCallbackWrapper() can also accept custom implementation of a callback wrapper.

// Custom implementation of a callback wrapper
const custom = {
    'callback': function() { /* not shown */ },
    'binding': undefined,
    'arguments': [],
    'with': function() { /* not shown */ },
    'hasArguments': function() { /* not shown */ },
    'bind': function() { /* not shown */ },
    'hasBinding': function() { /* not shown */ },
    'call': function() { /* not shown */ },
};

isCallbackWrapper(custom); // true

See the source code of isCallbackWrapper() for additional details.

Edit page
Last Updated:
Contributors: alin