All Projects β†’ chuntaro β†’ emacs-promise

chuntaro / emacs-promise

Licence: other
Promises/A+ for Emacs

Programming Languages

emacs lisp
2029 projects

Projects that are alternatives of or similar to emacs-promise

co demo
A step-by-step guide about how to avoid callback hell with ES6 Promises + generators (aka make your own "co")
Stars: ✭ 17 (-69.64%)
Mutual labels:  promise
eslint-config-welly
😎 βš™οΈ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (-62.5%)
Mutual labels:  promise
hangman-game
A responsive hangman game built with vanilla javascript, html, and css. Animated with GSAP and canvas animations.
Stars: ✭ 22 (-60.71%)
Mutual labels:  promise
nodeJS examples
Server, routing, db examples using NodeJS v6
Stars: ✭ 34 (-39.29%)
Mutual labels:  promise
repromised
🀝 Declarative promise resolver as a render props component
Stars: ✭ 20 (-64.29%)
Mutual labels:  promise
p-cache
Decorator to memoize the results of async functions via lru-cache.
Stars: ✭ 21 (-62.5%)
Mutual labels:  promise
miniprogram-network
Redefine the Network API of Wechat MiniProgram (ε°η¨‹εΊη½‘η»œεΊ“)
Stars: ✭ 93 (+66.07%)
Mutual labels:  promise
conquerant
lightweight async/await for Clojure
Stars: ✭ 31 (-44.64%)
Mutual labels:  promise
relaks
Asynchrounous React component
Stars: ✭ 49 (-12.5%)
Mutual labels:  promise
transceiver
Channel based event bus with request/reply pattern, using promises. For node & browser.
Stars: ✭ 25 (-55.36%)
Mutual labels:  promise
PiedPiper
A small set of classes and functions to make easy use of Futures, Promises and async computation in general. All written in Swift for iOS 10+, WatchOS 3, tvOS and Mac OS X apps.
Stars: ✭ 44 (-21.43%)
Mutual labels:  promise
rmfr
Node.js implementation of rm -fr – recursive removal of files and directories
Stars: ✭ 23 (-58.93%)
Mutual labels:  promise
sequence-as-promise
Executes array of functions as sequence and returns promise
Stars: ✭ 23 (-58.93%)
Mutual labels:  promise
PromisedFuture
A Swift based Future/Promises framework to help writing asynchronous code in an elegant way
Stars: ✭ 75 (+33.93%)
Mutual labels:  promise
Promise.allSettled
ES Proposal spec-compliant shim for Promise.allSettled
Stars: ✭ 93 (+66.07%)
Mutual labels:  promise
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (-67.86%)
Mutual labels:  promise
best-queue
Queue in runtime based promise
Stars: ✭ 26 (-53.57%)
Mutual labels:  promise
parcel-plugin-goodie-bag
provides the Promise and fetch goodies needed for IE(11) support w/ parcel bundle loading
Stars: ✭ 15 (-73.21%)
Mutual labels:  promise
iworker
Promise-based wrapper for worker_threads
Stars: ✭ 18 (-67.86%)
Mutual labels:  promise
lightflow
A tiny Promise-inspired control flow library for browser and Node.js.
Stars: ✭ 29 (-48.21%)
Mutual labels:  promise

Promises/A+ for Emacs

This is a simple implementation of Promises/A+.

This implementation ported the following Promises/A+ implementation faithfully.
https://github.com/then/promise

  • The same API as JavaScript version Promise can be used.
  • then, catch, resolve, reject, all, race, etc...
  • supports "thenable"
  • supports "Inheritance of Promise"
  • supports "rejection-tracking"

For detailed tutorials on its use, see www.promisejs.org (JavaScript).

Promises/A+ for Emacs is used in Async/Await for Emacs, so you can use Async/Await for asynchronous programming.

Installation

You can install from MELPA using package.el.
The package name is promise.

Usage

See promise-examples.el for details.

(require 'promise)

(defun do-something-async (delay-sec value)
  "Return `Promise' to resolve the value asynchronously."
  (promise-new (lambda (resolve _reject)
                 (run-at-time delay-sec
                              nil
                              (lambda ()
                                (funcall resolve value))))))

(defun example4 ()
  "All processes are asynchronous Promise chain."
  (promise-chain (do-something-async 1 33)
    (then (lambda (result)
            (message "first result: %s" result)
            (do-something-async 1 (* result 2))))

    (then (lambda (second-result)
            (message "second result: %s" second-result)
            (do-something-async 1 (* second-result 2))))

    (then (lambda (third-result)
            (message "third result: %s" third-result)))))

An example using `url-retrieve 'as a more complicated example.

(require 'promise)
(require 'url-http)
(require 'xml)
(require 'dom)

(defun xml-retrieve (url)
  "Return `Promise' to resolve with XML object obtained by HTTP request."
  (promise-new
   (lambda (resolve reject)
     (url-retrieve url
                   (lambda (status)
                     ;; All errors are reliably captured and rejected with appropriate values.
                     (if (plist-get status :error)
                         (funcall reject (plist-get status :error))
                       (condition-case ex
                           (with-current-buffer (current-buffer)
                             (if (not (url-http-parse-headers))
                                 (funcall reject (buffer-string))
                               (search-forward-regexp "\n\\s-*\n" nil t)
                               (funcall resolve (xml-parse-region))))
                         (error (funcall reject ex)))))))))

(defun get-text-first-tag (xml tag)
  "Returns the first text that matches TAG in XML."
  (decode-coding-string (dom-text (cl-first (dom-by-tag xml tag)))
                        'utf-8))

(defun get-short-text-first-tag (xml tag)
  "Truncate the text obtained with `get-text-first-tag'."
  (concat (truncate-string-to-width (get-text-first-tag xml tag) 64)
          " ..."))

(defun wait-seconds (seconds fn &rest args)
  "Return `Promise' to execute the function after the specified time."
  (promise-new (lambda (resolve _reject)
                 (run-at-time seconds
                              nil
                              (lambda ()
                                (funcall resolve (apply fn args)))))))

(defun example12 ()
  "Example using `xml-retrieve'."
  (let ((wikipedia-url (concat "https://en.wikipedia.org/w/api.php"
                               "?format=xml&action=query&prop=extracts"
                               "&exintro=&explaintext=&titles=")))
    (promise-chain (promise-all
                    (vector
                     (xml-retrieve (concat wikipedia-url (url-encode-url "GNU")))
                     ;; Request after 2 seconds for load reduction.
                     (wait-seconds 2
                                   #'xml-retrieve
                                   (concat wikipedia-url (url-encode-url "Emacs")))))
      (then (lambda (xmls)
              (message "%s" (get-short-text-first-tag (aref xmls 0) 'extract))
              (message "%s" (get-short-text-first-tag (aref xmls 1) 'extract))))

      (promise-catch (lambda (reason)
                       (message "promise-catch: %s" reason))))))

Tests

$ cask install
$ cask exec ert-runner
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].