Call to jquery ajax – .fail vs. :error

The two options are equivalent. However, the promise-style interface (.fail() and .done()) allow you to separate the code creating the request from the code handling the response. You can write a function that sends an AJAX request and returns the jqXHR object, and then call that function elsewhere and add a handler. When combined with … Read more

How to add delay to promise inside then [duplicate]

Return a promise from the then handler that waits: .then(() => new Promise(resolve => setTimeout(resolve, 1000))) If you want to “pass through” the value of the promise, then .then(x => new Promise(resolve => setTimeout(() => resolve(x), 1000))) To avoid this boilerplate everywhere, write a utility function: function sleeper(ms) { return function(x) { return new Promise(resolve … Read more

Set timeout for HTTPClient get() request

There are two different ways to configure this behavior in Dart Set a per request timeout You can set a timeout on any Future using the Future.timeout method. This will short-circuit after the given duration has elapsed by throwing a TimeoutException. try { final request = await client.get(…); final response = await request.close() .timeout(const Duration(seconds: … Read more

How do I ‘validate’ on destroy in rails

You can raise an exception which you then catch. Rails wraps deletes in a transaction, which helps matters. For example: class Booking < ActiveRecord::Base has_many :booking_payments …. def destroy raise “Cannot delete booking with payments” unless booking_payments.count == 0 # … ok, go ahead and destroy super end end Alternatively you can use the before_destroy … Read more

Difference between after_create, after_save and after_commit in rails callbacks

You almost got it right. However there is one major difference between after_commit and after_create or after_save i.e. In the case of after_create, this will always be before the call to save (or create) returns. Rails wraps every save inside a transaction and the before/after create callbacks run inside that transaction (a consequence of this … Read more

How to pass a callback as a parameter into another function

Yup. Function references are just like any other object reference, you can pass them around to your heart’s content. Here’s a more concrete example: function foo() { console.log(“Hello from foo!”); } function caller(f) { // Call the given function f(); } function indirectCaller(f) { // Call `caller`, who will in turn call `f` caller(f); } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)