Git Product home page Git Product logo

dezalgo's Introduction

dezalgo

Contain async insanity so that the dark pony lord doesn't eat souls

See this blog post.

USAGE

Pass a callback to dezalgo and it will ensure that it is always called in a future tick, and never in this tick.

var dz = require('dezalgo')

var cache = {}
function maybeSync(arg, cb) {
  cb = dz(cb)

  // this will actually defer to nextTick
  if (cache[arg]) cb(null, cache[arg])

  fs.readFile(arg, function (er, data) {
    // since this is *already* defered, it will call immediately
    if (er) cb(er)
    cb(null, cache[arg] = data)
  })
}

dezalgo's People

Contributors

dependabot[bot] avatar isaacs avatar mvayngrib avatar othiym23 avatar wraithgar avatar xhmikosr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dezalgo's Issues

bug in readme?

there is this

    if (er) cb(er)
    cb(null, cache[arg] = data)

did you mean this instead

    if (er) return cb(er)
    cb(null, cache[arg] = data)

Ryan Dahl, God among Gods, eschewed shorthand in favor of

    if (er) {
       return cb(er)
    }
    cb(null, cache[arg] = data)

Any ideas how to detect if callback is not called

For months i'm thinking how this can be done, even is it possible? Hm, don't know but i believe it is.

And I think that dezalgo is the right place for this.

previous discussion https://redd.it/59fvap

Last given snippet there works, but only if you does not use dezalgo (aka if not ensured that the callback is called in the next tick).

example

var once = require('once')
var dezalgo = require('dezalgo')

function task (fn) {
  var called = false
  function done (e) {
    called = true
    if (e) return console.log('err')
    console.log('ok')
  }

  fn(once(done))
  if (!called) console.log('callback is not called')
}

task(function (cb) {
  console.log('actual')
})
// signals that the `cb` is not called

But when we wrap the done with dezalgo we have a problem, logically. Because if it is called, it will be called in the next tick, so that if(!called) isn't in the correct time and the message is still outputed.

So such detection should be done in the dezalgo, because it calls the callback in the next tick. Or maybe in once.

ERROR come out this : 'code ENOENT, errno -2 npm, syscall rename'

i dont know what im doing wrong. i try install plugin in and suddenly this error come out. help me please. because i try so many thing. nothing help me. And it also make me cannot install any npm.

npm ERR! path /Users/.../.../.../.../node_modules/npm/node_modules/dezalgo/node_modules/asap
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename '/Users/.../.../.../.../node_modules/npm/node_modules/dezalgo/node_modules/asap' -> '/Users/.../.../.../.../node_modules/npm/node_modules/dezalgo/node_modules/.asap.DELETE'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

im running on

$ node -v
v9.4.0
$ npm -v
5.6.0
$ ionic -v
3.19.0
$ cordova -v
8.0.0

No such file or directory renaming .dezalgo.DELETE

Hi there!

When installing/updating a project of mine using npm install, I ran across the following problem with dezalgo:

npm ERR! enoent ENOENT: no such file or directory, rename '/Users/sander/Repositories/project/node_modules/npm/node_modules/dezalgo' -> '/Users/sander/Repositories/project/node_modules/npm/node_modules/.dezalgo.DELETE'
npm ERR! enoent This is related to npm not being able to find a file.

This seems to have occured out of the blue, so I wondered if this has anything to do with the dezalgo package?


Full debug log:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node',
1 verbose cli   '/usr/local/bin/npm',
1 verbose cli   'i',
1 verbose cli   '--reinstall' ]
2 info using [email protected]
3 info using [email protected]
4 verbose npm-session 09a7132d3aa76b8c
[...]
15 silly idealTree [email protected]
15 silly idealTree ├─┬ [email protected]
15 silly idealTree │ ├── [email protected]
[...]
18 silly diffTrees add [email protected]
[...]
25 silly decomposeActions preinstall [email protected]
26 silly decomposeActions build [email protected]
27 silly decomposeActions install [email protected]
28 silly decomposeActions postinstall [email protected]
29 silly decomposeActions finalize [email protected]
30 silly decomposeActions refresh-package-json [email protected]
[...]
70 silly finalize /Users/sander/Repositories/project/node_modules/npm/node_modules/dezalgo
[...]
72 silly saveTree [email protected]
72 silly saveTree ├─┬ [email protected]
72 silly saveTree │ ├─┬ [email protected]
72 silly saveTree │ │ ├─┬ [email protected]
[...]
74 verbose stack Error: ENOENT: no such file or directory, rename '/Users/sander/Repositories/project/node_modules/npm/node_modules/dezalgo' -> '/Users/sander/Repositories/project/node_modules/npm/node_modules/.dezalgo.DELETE'
75 verbose cwd /Users/sander/Repositories/project
76 verbose Darwin 17.3.0
77 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "i" "--reinstall"
78 verbose node v9.2.0
79 verbose npm  v5.6.0
80 error path /Users/sander/Repositories/project/node_modules/npm/node_modules/dezalgo
81 error code ENOENT
82 error errno -2
83 error syscall rename
84 error enoent ENOENT: no such file or directory, rename '/Users/sander/Repositories/project/node_modules/npm/node_modules/dezalgo' -> '/Users/sander/Repositories/project/node_modules/npm/node_modules/.dezalgo.DELETE'
85 error enoent This is related to npm not being able to find a file.
86 verbose exit [ -2, true ]

[Question] Why use asap instead of process.nextTick

As you said in your blog:

This uses Node's synthetic deferral function: process.nextTick, which runs the supplied callback at the end of the current run-to-completion. You can also use setImmediate, but that's slightly slower. (Yes, yes, it's named badly; setImmediate is slightly less "immediate" and more "next" than nextTick, but this is an accident of history we cannot correct without a time machine.)

So, why in the implementation of this library to resolve the zalgo problem you prefer use asap library? Maybe for more crossover browser compatibility?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.