V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
newghost
V2EX  ›  Node.js

要设计一个接口,大家觉得哪种看上去舒服点?

  •  
  •   newghost · Feb 8, 2020 · 7414 views
    This topic created in 2269 days ago, the information mentioned may be changed or developed.

    刚注意到代码在 node.js 节点下很丑,大家克服一下

    A: 连写型

    db.upsert('article', {
        id      : 1
      , title   : 'Hello world'
      , poster  : 'kris'
      , visit   : 100
    }).error(function(err) {
       ...
    }).done(function(count) {
       ...
    })
    
    db.remove('article', { id: 1 })
      .error(function(err) {
          ...
      })
      .done(function() {
          ...
      })
    
    
    db.find('article', {poster : 'kris'})
      .between('visit', 100, 200)
      .range(100, 200)
      .done(function(err, rows, count) {
        ....
      })
    

    B: 回调型

    oncedb.upsert('article', {
        id      : 1
      , title   : 'Hello world'
      , poster  : 'kris'
      , visit   : 100
    }, function(err) {
       ... 
    })
    
    db.remove('article', { id: 1 }, function(err) {
       ...
    })
    
    
    db.find('article', {poster : 'kris'}, function(err, rows) {
      if (err) {
        console.log(err)
        return
      }
    
      console.log(rows)
    
    }, { between: ['visit', 100, 200], range: [0, 100] })
    

    看到现在很多库都用 A,node.js 标准 API 基本上是 B,大家喜欢哪一种? 大家觉得流行风格会不会变?

    10 replies    2020-02-09 12:42:14 +08:00
    lsvih
        1
    lsvih  
       Feb 8, 2020
    当然链式调用更香啦
    gimp
        2
    gimp  
       Feb 8, 2020
    我喜欢 async / await ...
    secondwtq
        3
    secondwtq  
       Feb 8, 2020 via iPhone   ❤️ 2
    老实用 Promise 不好么……
    HarryYu
        4
    HarryYu  
       Feb 8, 2020
    promise +1
    yafoo
        5
    yafoo  
       Feb 8, 2020 via Android
    我喜欢 async / await ...
    ayase252
        6
    ayase252  
       Feb 8, 2020
    async await
    yimity
        7
    yimity  
       Feb 8, 2020
    写成 Promise,然后觉得 then 不好用可以 async await。
    wanguorui123
        8
    wanguorui123  
       Feb 9, 2020 via iPhone
    用 callback 转 Promise 转 await,最终用 await
    WilliamLin
        9
    WilliamLin  
       Feb 9, 2020
    async await
    wildnode
        10
    wildnode  
       Feb 9, 2020
    这个节点下的代码块样式有点影响体验啊
    另外发现这个节点下评论输入框 focus 的时候 背景会变白
    不知道是 Bug 还是 Feature
    @Livid
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6111 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 02:08 · PVG 10:08 · LAX 19:08 · JFK 22:08
    ♥ Do have faith in what you're doing.