Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB

пре 5 година
  1. ;;; request-deferred.el --- Wrap request.el by deferred -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; URL: https://github.com/tkf/emacs-request
  5. ;; Package-Version: 20181129.317
  6. ;; Package-Requires: ((deferred "0.3.1") (request "0.2.0"))
  7. ;; Version: 0.2.0
  8. ;; This file is NOT part of GNU Emacs.
  9. ;; request-deferred.el is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; request-deferred.el is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with request-deferred.el.
  19. ;; If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;
  22. ;;; Code:
  23. (require 'request)
  24. (require 'deferred)
  25. (defun request-deferred (url &rest args)
  26. "Send a request and return deferred object associated with it.
  27. Following deferred callback takes a response object regardless of
  28. the response result. To make sure no error occurs during the
  29. request, check `request-response-error-thrown'.
  30. Arguments are the same as `request', but COMPLETE callback cannot
  31. be used as it is used for starting deferred callback chain.
  32. Example::
  33. (require 'request-deferred)
  34. (deferred:$
  35. (request-deferred \"http://httpbin.org/get\" :parser 'json-read)
  36. (deferred:nextc it
  37. (lambda (response)
  38. (message \"Got: %S\" (request-response-data response)))))
  39. "
  40. (let* ((d (deferred:new #'identity))
  41. (callback-post (apply-partially
  42. (lambda (d &rest args)
  43. (deferred:callback-post
  44. d (plist-get args :response)))
  45. d)))
  46. ;; As `deferred:errorback-post' requires an error object to be
  47. ;; posted, use `deferred:callback-post' for success and error
  48. ;; cases.
  49. (setq args (plist-put args :complete callback-post))
  50. (apply #'request url args)
  51. d))
  52. (provide 'request-deferred)
  53. ;;; request-deferred.el ends here