Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 9ec3c4f4
authored
May 05, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Put the promise util methods in the bottom
1 parent
f8efb9f9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
44 deletions
lib/promise.js
lib/promise.js
View file @
9ec3c4f
...
...
@@ -64,7 +64,7 @@ SequelizePromise.all = function(promises) {
promise
.
$sql
.
forEach
(
function
(
sql
)
{
resolved
.
emit
(
'sql'
,
sql
);
});
}
}
});
return
resolved
;
...
...
@@ -82,26 +82,14 @@ SequelizePromise.settle = function (promises) {
promise
.
$sql
.
forEach
(
function
(
sql
)
{
settled
.
emit
(
'sql'
,
sql
);
});
});
}
});
return
settled
;
}
function
tryCatchApply
(
fn
,
args
,
receiver
)
{
try
{
return
fn
.
apply
(
receiver
,
args
);
}
catch
(
e
)
{
return
{
e
:
e
};
}
}
SequelizePromise
.
method
=
function
(
fn
)
{
if
(
typeof
fn
!==
"function"
)
{
throw
new
TypeError
(
NOT_FUNCTION_ERROR
);
}
return
function
Promise$_method
()
{
var
value
=
tryCatchApply
(
fn
,
Array
.
prototype
.
slice
.
apply
(
arguments
),
this
);
...
...
@@ -112,7 +100,7 @@ SequelizePromise.method = function (fn) {
};
};
Promise
.
prototype
.
_resolveFromSyncValue
=
function
(
value
)
{
Sequelize
Promise
.
prototype
.
_resolveFromSyncValue
=
function
(
value
)
{
if
(
value
&&
value
.
hasOwnProperty
(
'e'
))
{
this
.
_cleanValues
();
this
.
_setRejected
();
...
...
@@ -121,7 +109,7 @@ Promise.prototype._resolveFromSyncValue = function(value) {
}
else
{
var
maybePromise
=
Promise
.
_cast
(
value
,
void
0
);
if
(
maybePromise
instanceof
Promise
)
{
if
(
maybePromise
instanceof
Promise
||
maybePromise
instanceof
SequelizePromise
)
{
this
.
_follow
(
maybePromise
);
}
else
{
...
...
@@ -132,7 +120,7 @@ Promise.prototype._resolveFromSyncValue = function(value) {
}
};
SequelizePromise
.
attempt
=
SequelizePromise
[
"try"
]
=
function
(
fn
,
args
,
ctx
)
{
SequelizePromise
.
attempt
=
SequelizePromise
.
try
=
function
(
fn
,
args
,
ctx
)
{
var
value
=
tryCatchApply
(
fn
,
args
,
ctx
)
var
ret
=
new
SequelizePromise
(
INTERNAL
);
...
...
@@ -160,29 +148,27 @@ SequelizePromise.promisify = function (callback, receiver) {
var
promise
=
new
SequelizePromise
(
INTERNAL
);
promise
.
_setTrace
(
void
0
);
var
fn
=
function
PromiseResolver$_callback
(
err
,
value
)
{
if
(
err
)
{
// var wrapped = wrapAsRejectionError(maybeWrapAsError(err));
var
wrapped
=
err
promise
.
_attachExtraTrace
(
wrapped
);
promise
.
_reject
(
wrapped
);
if
(
err
)
{
var
wrapped
=
maybeWrapAsError
(
err
);
promise
.
_attachExtraTrace
(
wrapped
);
promise
.
_reject
(
wrapped
);
}
else
{
if
(
arguments
.
length
>
2
)
{
promise
.
_fulfill
(
Array
.
prototype
.
slice
.
call
(
arguments
,
1
));
}
else
{
if
(
arguments
.
length
>
2
)
{
promise
.
_fulfill
(
Array
.
prototype
.
slice
.
call
(
arguments
,
1
));
}
else
{
promise
.
_fulfill
(
value
);
}
promise
.
_fulfill
(
value
);
}
}
}
try
{
callback
.
apply
(
_receiver
,
withAppended
(
arguments
,
fn
));
callback
.
apply
(
_receiver
,
withAppended
(
arguments
,
fn
));
}
catch
(
e
)
{
// var wrapped = maybeWrapAsError(e);
var
wrapped
=
e
promise
.
_attachExtraTrace
(
wrapped
);
promise
.
_reject
(
wrapped
);
var
wrapped
=
maybeWrapAsError
(
e
);
promise
.
_attachExtraTrace
(
wrapped
);
promise
.
_reject
(
wrapped
);
}
return
promise
;
}
...
...
@@ -190,17 +176,6 @@ SequelizePromise.promisify = function (callback, receiver) {
return
promisified
;
}
function
withAppended
(
target
,
appendee
)
{
var
len
=
target
.
length
;
var
ret
=
new
Array
(
len
+
1
);
var
i
;
for
(
i
=
0
;
i
<
len
;
++
i
)
{
ret
[
i
]
=
target
[
i
];
}
ret
[
i
]
=
appendee
;
return
ret
;
}
// Need to hack _then to make sure our promise is chainable
SequelizePromise
.
prototype
.
_then
=
function
(
didFulfill
,
...
...
@@ -406,4 +381,37 @@ SequelizePromise.prototype.proxySql = function(promise) {
});
};
// Utility methods
function
withAppended
(
target
,
appendee
)
{
var
len
=
target
.
length
;
var
ret
=
new
Array
(
len
+
1
);
var
i
;
for
(
i
=
0
;
i
<
len
;
++
i
)
{
ret
[
i
]
=
target
[
i
];
}
ret
[
i
]
=
appendee
;
return
ret
;
}
function
isPrimitive
(
val
)
{
return
val
==
null
||
val
===
true
||
val
===
false
||
typeof
val
===
"string"
||
typeof
val
===
"number"
;
}
function
maybeWrapAsError
(
maybeError
)
{
if
(
!
isPrimitive
(
maybeError
))
return
maybeError
;
return
new
Error
(
asString
(
maybeError
));
}
function
tryCatchApply
(
fn
,
args
,
receiver
)
{
try
{
return
fn
.
apply
(
receiver
,
args
);
}
catch
(
e
)
{
return
{
e
:
e
};
}
}
module
.
exports
=
SequelizePromise
;
\ No newline at end of file
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment