How would one do async JavaScript getters and setters?

前端 未结 3 626
一生所求
一生所求 2021-01-03 18:37

Think of how Rails, e.g. allows you to define a property as associated with another:

class Customer < ActiveRecord::Base
  has_many :orders
end

3条回答
  •  清酒与你
    2021-01-03 19:28

    Here's how you could implement your get orders function

    function get(name) {
        return new Promise(function(resolve, reject) {
            db.find("orders", {customer: name}, function(err, data) {
                 if (err) reject(err);
                 else resolve(data);
            });
        });
    }
    

    You could call this function like

    customer.get("John").then(data => {
        // Process data here...
    }).catch(err => {
        // Process error here...
    });
    

提交回复
热议问题